28 Oct 2013

Group Session 2: Selenium WebDriver

Then some automated tests for user interface. We will try Selenium WebDriver and see where we get with it.

  • Install Selenium WebDriver
  • Learn to use it
  • Demonstrate it in the session
  • Bring two laptops with Java development environment for creating Selenium tests (ask after first session help for getting also cucumber setup on those machines). Prepare the environments so that we can just start writing tests.
Start from
  • http://www.seleniumhq.org/projects/webdriver/
  • youtube

5 comments:

  1. public class FrontPageSteps {

    WebDriver driver;

    @Before
    public void setup() {
    driver = new FirefoxDriver();

    }

    @After
    public void tearDown() {
    driver.quit();
    }

    @Given("^user has the frontpage$")
    @When("^user opens a page$")
    public void user_opens_a_page() throws Throwable {

    driver.get("http://www.matkahuolto.fi/en/");

    }

    @Then("^user sees the logo on the top left of the frontpage$")
    public void user_sees_the_logo_on_the_top_left_of_the_frontpage()
    throws Throwable {
    WebElement logo = driver.findElement(By.id("logo"));
    WebElement image = logo.findElement(By.tagName("img"));
    Assert.assertNotNull(logo);
    Assert.assertNotNull(image);
    Assert.assertTrue(image.isDisplayed());

    }

    @When("^user types departure to be Turku$")
    public void user_types_departure_to_be_Turku() throws Throwable {
    WebElement departure = driver.findElement(By
    .name("departureStopAreaName"));
    departure.sendKeys("Turku");
    }

    @When("^user types destination to be Helsinki$")
    public void user_types_destination_to_be_Helsinki() throws Throwable {

    WebElement arrival = driver.findElement(By.name("arrivalStopAreaName"));
    arrival.sendKeys("Helsinki");
    }

    @When("^user submits the search$")
    public void user_submits_the_search() throws Throwable {

    WebElement search = driver.findElement(By.name("search"));
    search.click();
    Thread.sleep(2000);
    }

    @Then("^timetable is opened$")
    public void timetable_is_opened() throws Throwable {
    // Express the Regexp above with the code you wish you had
    throw new PendingException();
    }

    }

    ReplyDelete
  2. Group 2 codes:

    package fi.ty.selenium;

    import static org.junit.Assert.*;

    import org.openqa.selenium.By;
    import org.openqa.selenium.JavascriptExecutor;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.support.ui.WebDriverWait;

    import com.google.common.base.Predicate;

    import cucumber.api.PendingException;
    import cucumber.api.java.en.*;

    public class StepDefinitions {

    WebDriver driver;
    final String baseUrl = "http://www.matkahuolto.fi/";
    private void waitForPage() {
    WebDriverWait wait = new WebDriverWait(driver, 5);
    wait.until(new Predicate() {
    public boolean apply(WebDriver arg0) {
    return ((JavascriptExecutor) driver).executeScript(
    "return document.readyState").equals("complete");
    }
    });
    }
    @When("^user opens a page$")
    public void user_opens_a_page() throws Throwable {
    // Express the Regexp above with the code you wish you had

    driver = new FirefoxDriver();
    driver.get(baseUrl);
    System.out.println("Open Matkahuolto.fi");

    }

    @Then("^user sees the logo on the top left of the frontpage$")
    public void user_sees_the_logo_on_the_top_left_of_the_frontpage() throws Throwable {
    // Express the Regexp above with the code you wish you had
    waitForPage();
    WebElement logo = driver.findElement(By.cssSelector(String.format("img[src *= '%s']", "/system/modules/fi.matkahuolto.frontend/resources/images/mh_logo_v2.gif")));

    assertNotNull(logo);
    assertTrue(logo.isDisplayed());
    if(logo.isDisplayed()){
    System.out.println("Logo is visible");
    }
    driver.quit();

    }
    @Given("^user opens the frontpage$")
    public void user_opens_the_frontpage() throws Throwable {

    driver = new FirefoxDriver();
    driver.get(baseUrl);
    System.out.println("Open Matkahuolto.fi");
    }

    @Given("^there is the timetable with the 'submit' button$")
    public void there_is_the_timetable_with_the_submit_button() throws Throwable {
    waitForPage();
    WebElement lähtö = driver.findElement(By.name("departureStopAreaName"));
    assertNotNull(lähtö);
    WebElement kohde = driver.findElement(By.name("arrivalStopAreaName"));
    assertNotNull(kohde);
    WebElement button = driver.findElement(By.id("scheduleSearch")).findElement(By.name("search"));
    assertNotNull(button);
    //System.out.println("Fields exist.");

    }

    @When("^user types travel information to timetable$")
    public void user_types_travel_information_to_timetable() throws Throwable {
    WebElement lähtö = driver.findElement(By.name("departureStopAreaName"));
    WebElement kohde = driver.findElement(By.name("arrivalStopAreaName"));

    lähtö.sendKeys("Turku");
    kohde.sendKeys("Helsinki");
    }

    @When("^user press the 'submit' button$")
    public void user_press_the_submit_button() throws Throwable {
    WebElement button = driver.findElement(By.id("scheduleSearch")).findElement(By.name("search"));
    button.click();
    }

    @Then("^page opens containing showing the timetable.$")
    public void page_opens_containing_showing_the_timetable() throws Throwable {
    assertTrue(driver.getCurrentUrl().contains("departureStopAreaName=Turku"));
    assertTrue(driver.getCurrentUrl().contains("arrivalStopAreaName=Helsinki"));
    System.out.println("alles gut");
    }
    }

    ReplyDelete
  3. features:
    frontpage-group1.feature:

    Feature: Frontpage
    In order to have more customers
    As a bus transport company owner
    I want to have a fancy web page
    Scenario: Logo on top left of the page
    When user opens a page
    Then user sees the logo on the top left of the frontpage

    Scenario: Newsfeed, 6 recent visible

    Scenario: timetable accessible from frontpage
    Given user opens the frontpage
    And there is the timetable with the 'submit' button
    When user types travel information to timetable
    And user press the 'submit' button
    Then page opens containing showing the timetable.

    Scenario Outline: timetable not accessible
    Given user types
    And user types
    When user presses submit button
    Then user geta
    Examples:
    |departure|destination|error message|
    | |Vantaa |Departure city missing|
    |Vantaa | |Arrival city missing|
    |Vantaa |Vantaa |Departure same as arrival|


    testi.feature:
    Feature: Frontpage
    Background:
    Given we have page
    Scenario: Logo on top left of the page
    Given we have logo
    And we have page
    When page loaded
    Then show logo on top left of the page

    Scenario: 6 recent news on the page
    Given we have news
    When page loaded
    Then show 6 most recent news on news feed

    Scenario: news feed update
    Given we have a news feed
    When new news available
    Then remove oldest news
    And add new news

    Scenario: contact information for purchasing tickets
    Given we have contact information
    And we have open hours information
    When page loaded
    Then show contact information and open hours

    Scenario: contact information for parcel services
    Given we have phone number for the parcel services
    And we have open hours of the parcel office
    When page loaded
    Then show the contact information and open hours

    Scenario: timetable accessible from front page
    Given we have timetable service
    When button is clicked
    Then show the timetable front page

    TIcket.feature:
    Feature: Ticket reservation
    Scenario: Search available connections
    Given we have reservation page
    And we have selected from location
    And to destination
    And date
    When information is confirmed
    Then list all availabSle connections for this selection

    Scenario: Show the available connections
    Given we a list of available connections
    When the page is loaded
    Then show the details

    Scenario: Selecting one connection
    Given the details of a chosen connection
    When user choice is made
    Then display all stops in connection and arrival times

    Scenario: browse connections
    Given a list of price details
    When we make the choice
    Then show the special price

    Scenario: ticket

    ReplyDelete
  4. https://github.com/Wiltzu/selenium-example - You can use this git/maven project as a base your experimenting with cucumber and selenium.

    ReplyDelete
  5. And here's the expert group's material:
    https://docs.google.com/presentation/d/1n-AwRZK_n2y5GAHN1Npw0_ZES9Kl-y5v_tAuwTT2J4I/edit?usp=sharing

    ReplyDelete