使用Selenium在不同的浏览器上“自动"运行JUnit测试. [英] Running JUnit Tests On different browsers using Selenium "Automatically"

查看:98
本文介绍了使用Selenium在不同的浏览器上“自动"运行JUnit测试.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在使用硒API,并已成功使用它在Firefox和chrome上进行测试.但是,要在两个浏览器上自动运行相同的单元测试,我需要做些什么.我试过将WebDrivers放在ArrayList<WebDriver> drivers对象中,但是如果我这样做,测试将无法正确运行.目前,在ArrayList中只有一个驱动程序,但仅对一个条目仍然无法使用.这是一些代码...

So I'm using the selenium API, and am using it successfully to test on firefox and chrome. But what would I need to do to run the same unit tests on both browsers automatically. I've tried putting the WebDrivers in an ArrayList<WebDriver> drivers object, but the tests don't run correctly if I do it this way. At the moment there is only one driver in the ArrayList, but it still will not work with just one entry. Here's some code...

package testsuites;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class BingTests extends BaseTestSuite{
//private WebDriver fireFoxDriver;
private WebDriver chromeDriver;
private WebDriver fireFoxDriver;
private  ArrayList<WebDriver> drivers;

@Before
public void setUp() throws Exception {
    fireFoxDriver = new FirefoxDriver();
    fireFoxDriver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    drivers.add(fireFoxDriver);
}

@Test
public void testGoogle(){
    for(WebDriver driver: drivers){
        driver.get("http://www.bing.com");
        driver.findElement(By.id("sb_form_q")).clear();
        driver.findElement(By.id("sb_form_q")).sendKeys("Selenium IDE");
        driver.findElement(By.id("sb_form_go")).click();
          driver.findElement(By.xpath("//ul[@id='wg0']/li[2]/div/div/h3/a/strong")).click();
        WebElement elem = driver.findElement(By.id("mainContent")); 
        assertTrue(elem.getText().contains("Selenium News"));
    }
}

@Test
public void isWikiContentCorrect(){
    for(WebDriver driver: drivers){
        driver.get("http://www.bing.com");
        driver.findElement(By.id("sb_form_q")).clear();
        driver.findElement(By.id("sb_form_q")).sendKeys("Saturn");
        driver.findElement(By.id("sb_form_go")).click();
        driver.findElement(By.linkText("Saturn - Wikipedia, the free encyclopedia")).click();
        driver.findElement(By.cssSelector("li.toclevel-1.tocsection-9 > a > span.toctext")).click();
        assertTrue(driver.getPageSource().contains("53 of which"));
    }
}

@Test
public void isWikiTitleCorrect(){
    for(WebDriver driver: drivers){
        driver.get("http://www.bing.com");
        driver.findElement(By.id("sb_form_q")).clear();
        driver.findElement(By.id("sb_form_q")).sendKeys("Saturn");
        driver.findElement(By.id("sb_form_go")).click();
        driver.findElement(By.linkText("Saturn - Wikipedia, the free encyclopedia")).click();
        assertEquals("Saturn - Wikipedia, the free encyclopedia", driver.getTitle());
    }
}

@Test
public void testDropDownWithSelenium(){
    for(WebDriver driver: drivers){
        driver.get("http://www.bing.com");
        driver.findElement(By.id("sb_form_q")).clear();
        driver.findElement(By.id("sb_form_q")).sendKeys("Neptunes moon");
        driver.findElement(By.partialLinkText("moons and rings")).click();
        driver.findElement(By.linkText("Neptune (planet) :: Neptune's moons       and rings -- Britannica Online ...")).click();
        List<WebElement> elems = driver.findElements(By.tagName("Input"));
        for(WebElement elem: elems){
            System.out.println(elem.getText());
        }
    }
}

@After
public void tearDown() throws Exception {
    for(WebDriver driver: drivers){
        driver.close();
    }
}
}

推荐答案

您采用了错误的做法.此实现需要在测试方法中使用不必要的循环代码.随着测试用例数量的增加,这种做法变得更加痛苦(以为测试用例数为100).

You employed wrong practice.This implementation requires unnecessary looping code within test method. As number of test cases get increase, this kind of practice becomes more painful (think 100s of test cases).

使用Selenium Grid或使用 QAF(以前称为ISFW).在Qmetry Automation框架(QAF)中,您可以在配置文件中对其进行设置以在不同的浏览器上运行. 例如

Use Selenium Grid or utilise QAF formerly ISFW. In Qmetry Automation Framework (QAF) you can set it in configuration file to run against different browsers. For example

<suite name="Test Automation" verbose="0" parallel="tests">

    <test name="Test on FF">
      <parameter name="browser" value="InternetExplorerWebDriver" />
    ...
    </test>
    <test name="Test on IE">
      <parameter name="browser" value="firefoxWebDriver" />
    ...
    </test>
</suit>

这篇关于使用Selenium在不同的浏览器上“自动"运行JUnit测试.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆