简体   繁体   中英

how to test multiple browser(versions) with selenium and junit

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?

What alternatives are there?

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

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

Use Selenium RC/Selenium Server. These come with the API's you will need to run remote tests in multiple browsers simply. Happy Hunting!

Check out the Selenide library. It's an open source wrapper for selenium that makes UI testing a breeze. Here's an example test.

@Test
public void userCanLoginByUsername() {
    open("/login");
    $(By.name("user.name")).setValue("johny");
    $("#submit").click();
    $(".loading_progress").should(disappear); // Waits until element disappears
    $("#username").shouldHave(text("Hello, Johny!")); // Waits until element gets text
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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