简体   繁体   English

如何让webdriver在每次测试后都不关闭浏览器窗口?

[英]How make webdriver not to close browser window after each test?

I'm new in both Selenium WebDriver and Java. 我是Selenium WebDriver和Java的新手。 I have some webservices on my site on page /someservice.php. 我的网站/someservice.php上有一些web服务。 I've wrote few tests on Selenuim and they work fine. 我在Selenuim上写了几个测试,但它们工作正常。 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). 现在我有2个测试,每个测试有3个参数(总共6个变体)。 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). 正如您在每个变体中所看到的,我创建了新的浏览器窗口,当我运行所有6个变体时,需要花费太多时间(最多80秒)。

How can I run all variants in one browser window to speed up my tests? 如何在一个浏览器窗口中运行所有变体以加快测试速度?

Just move contents of public static void close() method from your SiteClass to tearDownClass() method in DiamTest class. 只需将Site static中public static void close()方法的内容移动到DiamTest类中的tearDownClass()方法即可。 In this way the browser window will be closed when the class execution finished (because of @AfterClass annotation). 这样,当类执行完成时(由于@AfterClass注释),浏览器窗口将被关闭。 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) 将浏览器窗口初始化移动到setUpClass()方法也是一种很好的做法,该方法将在每个测试类之前执行(根据@BeforeClass注释)

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

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. 您需要做的是与所有测试共享您的帮助类,这意味着您应该在setUpClass方法中创建SiteClass的实例。 This method are annotated with @BeforeClass assuring your test class will create this method will be executed before all the test be executed. 此方法使用@BeforeClass注释,确保您的测试类将创建此方法,将在执行所有测试之前执行。

You can read more about @BeforeClass in jUnit doc : or have a simple overview in this response. 您可以在jUnit doc中阅读有关@BeforeClass的更多信息:或者在响应中有一个简单的概述。

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 . 提示:您应该阅读有关TestPyramid的信息

Since functional tests are expensive, you should care about what is really necessary test. 由于功能测试很昂贵,你应该关心什么是真正必要的测试。 This article is about this. 这篇文章就是这个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM