【问题标题】:How make webdriver not to close browser window after each test?如何让 webdriver 在每次测试后不关闭浏览器窗口?
【发布时间】:2015-06-29 15:23:54
【问题描述】:

我是 Selenium WebDriver 和 Java 的新手。我的网站页面/someservice.php 上有一些网络服务。我在 Selenuim 上写了一些测试,它们运行良好。代码示例(主类):

    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;
      }
    }

代码示例(测试类)

    @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();
    }

}

现在我有 2 个类似的测试,每个测试有 3 个参数(总共 6 个变体)。正如您在每个变体中看到的那样,我创建了新的浏览器窗口,并且当我运行所有 6 个变体时会花费太多时间(最多 80 秒)。

如何在一个浏览器窗口中运行所有变体以加快测试速度?

【问题讨论】:

    标签: java testing selenium junit


    【解决方案1】:

    只需将 public static void close() 方法的内容从您的 SiteClass 移动到 DiamTest 类中的 tearDownClass() 方法。这样,当类执行完成时,浏览器窗口将关闭(因为 @AfterClass 注释)。您的代码应如下所示:

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

    将浏览器窗口初始化移动到 setUpClass() 方法也是一个好习惯,该方法将在每个测试类之前执行(根据 @BeforeClass 注释)

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

    【讨论】:

    【解决方案2】:

    您需要做的是与所有测试共享您的帮助类,这意味着您应该在 setUpClass 方法中创建一个 SiteClass 实例。 此方法使用 @BeforeClass 注释,以确保您的测试类将创建此方法将在所有测试执行之前执行。

    您可以在 jUnit doc: 中阅读有关 @BeforeClass 的更多信息:或在 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));
            }
    
        }
    

    和:

        public class SiteClass {
                static WebDriver driver;
    
                public SiteClass() {
                    driver = new FirefoxDriver();
                }
    
                public void open(String url){
                    driver.get(url);
                }
    
                ...
    

    提示: 你应该阅读TestPyramid

    由于功能测试很昂贵,您应该关心什么是真正必要的测试。这篇文章就是关于这个的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-11
      • 1970-01-01
      • 2020-07-06
      • 1970-01-01
      • 1970-01-01
      • 2020-03-14
      • 1970-01-01
      • 2014-04-09
      相关资源
      最近更新 更多