Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Selenium 2.0 WebDriver in practice

I want to write Selenium test cases in JUnit and test my projects in multiple browsers and I would like to take advantage of the fact that all Selenium drivers implement the same interface.

Each test case should look like this:

package fm;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import static org.junit.Assert.*;

public class HomepageTest {

    @Test
    public void testTitle(WebDriver driver) {
        driver.get("http://localhost/");
        assertEquals("Foo", driver.getTitle());
    }

    @Test
    public void testSearchForm(WebDriver driver) {
        //...
    }

}

The passed WebDriver implementations should be controlled somewhere centrally. I'll probably need to override some of the JUnit behaviour and I hope it's possible.

I want to do it this way in order to avoid two things:

  • Code repetition: If each test case would initialize all tested browsers in @Before, the test suite would have a lot of repeated code that is hard to maintain.
  • Speed of the test suite: If I had centralized control over the order and passed WebDriver implementations, I could easily manage to open for example Firefox, run all test cases in it, close it and open the next browser. If each test case would manage to open and close browsers on its own, it would add a lot of time to each test run.

Anybody have an idea how should I do it? Thanks.

like image 947
Ondřej Mirtes Avatar asked Sep 03 '11 15:09

Ondřej Mirtes


2 Answers

In the Selenium project we inject what we need using http://code.google.com/p/selenium/source/browse/trunk/java/client/test/org/openqa/selenium/AbstractDriverTestCase.java and then our build calls the browser and we get tests running in it.

Have a look at our code base to get some inspiration!

like image 87
AutomatedTester Avatar answered Nov 07 '22 17:11

AutomatedTester


Please check with ISFW it supports selenium webdriver/remote webdriver as well as conventional (selenium1) rc way. You need to write code using regular selenium api for example

selenium.open(url);
selenium.type("loc", "text to type");
selenium.submit("loc");

Here is the working demo. Set browser String as per your requirement. The FW support selenium conventional way as well as selenium 2 webdriver. You need to set appropriate browser string in application properties. Following are different browser configurations for Firefox:

  • *firefox - required selenium server running on configured host/port if not found then fw will check/start one on locahost/port
  • firefoxDriver – will run directly with firefox web driver without selenium server
  • firefoxRemoteDriver - required selenium server running on configured host/port if not found then fw will check/start one on locahost/port, it will run test using firefox web driver on host machine

Same way for IE - *iexplore, *iehta, iexplorerDriver, iexplorerRemoteDriver and so on.

like image 21
user861594 Avatar answered Nov 07 '22 17:11

user861594