How to make webdriver not close the browser window after every test?

I am new to Selenium WebDriver and Java. I have web services in my site at /someservice.php page. I've written some tests in Selenuim and they work great. Example code (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;
      }
    }

      

Sample code (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 3 parameters each (6 options in total). As you can see in each option, I create a new browser window and when I run all 6 options it takes too long (up to 80 seconds).

How do I run all variations in one browser window to speed up my tests?

+3


source to share


2 answers


Just move the content of the public static void close () method from your SiteClass to the tearDownClass () method in the DiamTest class. Thus, the browser window will be closed when the class finishes executing (due to the @AfterClass annotation). Then your code should look like this:

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

      



It is also recommended to translate the initialization of the browser window into the setUpClass () method, which will be executed before each test class (according to the @BeforeClass annotation)

//DiamTest class
@BeforeClass
    public static void setUpClass() {
        //Here we initialize the firefox webdriver
        driver=new FirefoxDriver();
        driver.get(url);
    }

      

+1


source


What you need to do is share your help class with all your trials, this means that you have to instantiate SiteClass inside your setUpClass method . This method is annotated with @BeforeClass, ensuring that your test class creates this method is executed before the whole test is executed.

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

You will also need to rewrite some code a little to allow sharing of the driver with 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);
            }

            ...

      

Hint: You should read about TestPyramid .

Since functional tests are expensive, you should take care of what you really need to test. This article is about it.

0


source







All Articles