如何使用硒和junit测试多个浏览器(版本) [英] how to test multiple browser(versions) with selenium and junit

查看:93
本文介绍了如何使用硒和junit测试多个浏览器(版本)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚发现硒-很好的工具!
我计划运行/使用硒生成的junit4代码.但是我需要它与许多浏览器/网络驱动程序一起运行. 此用例是否有junit/java-pattern?我的第一个想法是使用@RunWith(Parameterized.class)并提供WebDrivers列表(该类的参数-可能作为列出浏览器和版本的外部文件提供?!).这是一个好主意吗?是否可以提供中心的@Parameters-方法供我所有的硒测试使用?

I just discovered selenium - a great tool!
I plan to run/use selenium-ide generated junit4 code. But I need it to run with many browsers/web drivers. Is there a junit/java-pattern for this use case? My first idea was to use @RunWith(Parameterized.class) and provide a List of WebDrivers (the parameter for the class - probably provided as an external file listing browsers and versions?!). Is this a good idea? Is it possible to provide a central @Parameters -method to be used by all my Selenium-tests?

有哪些替代方案?

也许可以更改Selenium导出的格式"以最大程度地减少手动更改?

Probably it is possible to change the "Format" that Selenium exports to minimize manual changes?

推荐答案

好吧,我确实需要不时切换驱动程序,所以我做到了:

Well, I do need to switch drivers from time to time, so I did this:

我在我自己的类中初始化与硒有关的东西-按应用程序的名称调用,并且吸气剂会接近驱动程序.调用类构造函数时,我使用驱动程序的枚举类型初始化:

I initialize selenium related stuff in my own Class - called by name of the application and the driver is approached by the getters. When calling my class constructor, I use enum type of driver to initialize with:

 private WebDriver driver;
 public TestUI(Environment.DriverToUse drv){
   switch (drv){
        case CHROME:{
            ChromeDriverService service = ChromeDriverService.createDefaultService();
            File file = new File(TestUI.class.getResource("/chromedriver.exe").toURI());
            System.setProperty(ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY, file.getAbsolutePath());                
            ChromeOptions options = new ChromeOptions();
            options.addArguments("--start-maximized");
            driver = new ChromeDriver(service,options);
            driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
            break;
        }
        case FIREFOX:{
            FirefoxProfile ffProfile = new FirefoxProfile();
            ffProfile.setPreference("browser.safebrowsing.malware.enabled", false);
            driver = new FirefoxDriver(ffProfile);
            driver.manage().window().setPosition(new Point(0, 0));
            java.awt.Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            Dimension dim = new Dimension((int) screenSize.getWidth(), (int) screenSize.getHeight());
            driver.manage().window().setSize(dim);
            driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
            break;

        }    

public WebDriver getDriver(){
 return driver;
}

当然我的Environment类看起来像这样

of course my Environment class looks like this

public class Environment {
public enum DriverToUse {FIREFOX, CHROME};
// .. and some other stuff, because I need to test on different environments, so I store here Environment URL for example

我的测试课看起来像这样

And my test class looks something like this

@Before
public static final Environment.DriverToUse USED_DRIVER = Environment.DriverToUse.FIREFOX;

@Test
public void testVersionNumber() throws Exception{

    TestUI testUI= new TestUI(USED_DRIVER);
    WebElement version = testUI.getDriver().findElement(By.id("the Id of element"));
    version.click();
    //...
}

这篇关于如何使用硒和junit测试多个浏览器(版本)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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