Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A good working example of Selenium2 and webdriver

I've been using selenium 1, but now want to migrate to selenium2/webdriver. To be honest, I find a little bit difficult to start with selenium2/webdriver. In essence I don't know how to work between page objects. Here is my example:

public class LoginPage {
    private final WebDriver driver;

    public LoginPage(WebDriver driver) {
        this.driver = driver; 
    }

    public void loginAs(String username, String password) {
        driver.get("http://url_to_my_webapp");        
        driver.findElement(By.id("username")).sendKeys(username);
        driver.findElement(By.id("pwd")).sendKeys(password);
        driver.findElement(By.className("button")).submit();                  
    }

    public static void main(String[] args){
        LoginPage login = new LoginPage(new FirefoxDriver());
        login.loginAs("user", "pass");
    }
}

Now, after user is logged in, a redirection to different page occurs. As far as I understand, I should now make a new page object that represents current page... The fact is I don't know how? Where can I find some good working examples which are going beyond "hello world" level? How should I continue this example?

Thanks in advance!

like image 677
babazumbula Avatar asked Apr 05 '11 21:04

babazumbula


People also ask

What is selenium WebDriver and how it works?

Selenium WebDriver is a web framework that permits you to execute cross-browser tests. This tool is used for automating web-based application testing to verify that it performs expectedly. Selenium WebDriver allows you to choose a programming language to create test scripts.

What is selenium2?

Selenium 2 is the combination of WebdDriver+SeleniumRC (Selenium1) which is based on selenium core. The core is removed from Selenium3 but supports Selenium RC indirectly through back-end Webdriver. WebDriver contributes its object-oriented API for Document Object Model (DOM) interaction and browser control.

What do we mean by Selenium 1 and Selenium 2?

Selenium 1 = Selenium Remote Control. Selenium 2 = Selenium 1 + WebDriver. Depends on the core libraries which could run on any browser which would support JavaScript. It runs JavaScript natively and test cases created can be run on different browsers.

How many Webdrivers are there in selenium?

There are four basic components of WebDriver Architecture: Selenium Language Bindings. JSON Wire Protocol. Browser Drivers.


1 Answers

This question is pretty old, but I thought it might still be worth sharing.

Generally, I'll first create the required page object classes. Then I create a separate class for the test logic, where you would put your 'user workflow' of clicks and other page interactions. From the sample code provided, I'm assuming that this class would replace main(). This is also the class where I include things like testNG/junit, test annotations, and dataProviders (not strictly required, but if you use those things, that may be helpful to note) In this class, you can instantiate the classes for the pages you will interact with as you need them since the webdriver object you created controls the browser, not the page classes.

Doing things this way allows for simple changes to test workflows, and also to the page objects in case the actual pages are changed, or you just have new test requirements.

My favorite side effect of this method is that the class with the workflow can be a very readable 'script' of the test with all of the ugly details in the actual tests hidden under calls like loginPage.Login() and loginPage.LoginSucceeded() so a casual pass doesn't see the details of user credential lookups, handling 404's/400's, finding and clicking the login button, etc.

like image 68
robert arles Avatar answered Sep 19 '22 14:09

robert arles