3

I'm new in both Selenium WebDriver and Java. I have some webservices on my site on page /someservice.php. I've wrote few tests on Selenuim and they work fine. Code example (Main Class):

    public class SiteClass {
    static WebDriver driver;
    private static boolean findElements(String xpath,int timeOut ) {
public static void open(String url){
        //Here we initialize the firefox webdriver
        driver=new FirefoxDriver();
        driver.get(url);
    }
    public static void close(){
        driver.close();
    }
            WebDriverWait wait = new WebDriverWait( driver, timeOut );
            try {
                if( wait.until( ExpectedConditions.visibilityOfElementLocated( By.xpath( xpath ) ) ) != null ) {
                    return true;
                } else {
                    return false;
                }
            } catch( TimeoutException e ) {
                return false;
            }}
    public static Boolean CheckDiameter(String search,String result){
          driver.findElement(By.xpath("//input[@id='search_diam']")).sendKeys(search);
          WebDriverWait wait = new WebDriverWait(driver, 5);
          WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[@class='ac_results'][last()]/ul/li")));
          WebElement searchVariant=driver.findElement(By.xpath("//div[@class='ac_results'][last()]/ul/li"));
          Actions action = new Actions(driver);
          action.moveToElement(searchVariant).perform();
          driver.findElement(By.xpath("//li[@class='ac_over']")).click();
          Boolean iselementpresent = findElements(result,5);
          return iselementpresent;
      }
    }

Code Example (Test Class)

    @RunWith(Parameterized.class)
public class DiamTest {@Parameters
    public static Collection<Object[]> diams() {
        return Arrays.asList(new Object[][] {
            { "111", "//div[@class='jGrowl-message']",true},
            { "222", "//div[@class='jGrowl-message']",false},
            { "333", "//div[@class='jGrowl-message']",true},
        });
    }
    private String inputMark;
    private String expectedResult;
    private Boolean assertResult;

    public DiamTest(String mark, String result, boolean aResult) {
        inputMark=mark;
        expectedResult=result;
        assertResult=aResult;
    }

    @BeforeClass
    public static void setUpClass() {
    }

    @AfterClass
    public static void tearDownClass() {
    }

    /**
     * Test of CheckDiameter method, of class CableRu.
     */
    @Test
    public void testCheckDiameter() {
        SiteClass obj=new SiteClass();
         obj.open("http://example.com/services.php");
        assertEquals(assertResult, obj.CheckDiameter(inputMark, expectedResult));
        obj.close();
    }

}

Now I have 2 tests like that with 3 parameters each (total 6 variants). As you can see in every variant I create new browser window and when I run all 6 variants that take too much time (up to 80 seconds).

How can I run all variants in one browser window to speed up my tests?

Viktor Malyi
  • 2,298
  • 2
  • 23
  • 40
Vladimir Zotov
  • 571
  • 6
  • 16

2 Answers2

1

Just move contents of public static void close() method from your SiteClass to tearDownClass() method in DiamTest class. In this way the browser window will be closed when the class execution finished (because of @AfterClass annotation). Your code then should look like this:

//DiamTest class
@AfterClass
    public static void tearDownClass() {
        driver.close();
    }

It's also a good practice to move browser window initialization to setUpClass() method which will be executed before each test class (according to @BeforeClass annotation)

//DiamTest class
@BeforeClass
    public static void setUpClass() {
        //Here we initialize the firefox webdriver
        driver=new FirefoxDriver();
        driver.get(url);
    }
Viktor Malyi
  • 2,298
  • 2
  • 23
  • 40
  • You can find more about those annotations here: http://stackoverflow.com/questions/20295578/difference-between-before-and-beforeclass?lq=1 – Viktor Malyi Apr 22 '15 at 09:25
0

What you need to do is share your help class with all your tests, this mean, you should create an instance of SiteClass inside your setUpClass method. This method are annotated with @BeforeClass assuring your test class will create this method will be executed before all the test be executed.

You can read more about @BeforeClass in jUnit doc: or have a simple overview in this response.

You will also need do some rewrite some code to allow share the driver with the another test, something like this:

    @RunWith(Parameterized.class)
    public class DiamTest {

            @Parameters
        public static Collection<Object[]> diams() {
            return Arrays.asList(new Object[][] {
                { "111", "//div[@class='jGrowl-message']",true},
                { "222", "//div[@class='jGrowl-message']",false},
                { "333", "//div[@class='jGrowl-message']",true},
            });
        }
        private String inputMark;
        private String expectedResult;
        private Boolean assertResult;

        private static SiteUtil siteUtil; 

        public DiamTest(String mark, String result, boolean aResult) {
            inputMark=mark;
            expectedResult=result;
            assertResult=aResult;
        }

        @BeforeClass
        public static void setUpClass() {
            siteUtil = new SiteUtil();
        }

        @AfterClass
        public static void tearDownClass() {
            siteUtil.close();
        }

        @Test
        public void testCheckDiameter() {
            siteUtil.open("http://example.com/services.php");
            assertEquals(assertResult, obj.CheckDiameter(inputMark, expectedResult));
        }

    }

and:

    public class SiteClass {
            static WebDriver driver;

            public SiteClass() {
                driver = new FirefoxDriver();
            }

            public void open(String url){
                driver.get(url);
            }

            ...

Tip: You should read about the TestPyramid.

Since functional tests are expensive, you should care about what is really necessary test. This article is about this.

Community
  • 1
  • 1
bcfurtado
  • 181
  • 2
  • 12