Monday, 31 August 2015

8. Page Object

Page Object


1. How to start

2. Example with Google search



1. How to start


Refresh in memory 1. Introduction

Pay attention to:
Vimeo: How to write UI test in 10 minutes
Selenide site: http://selenide.org/documentation/page-objects.html


2. Example with Google search


You can find concise examples for Classic Selenium Page Object, Selenide Page Object, Selenide Page Object with fields here: selenide-examples (official source) .

The example below is just another variation and not pretending to be something more than simple quick example with a chain of methods inside the test. 

Example:



// test/GoogleTest.java

package ua.in.externalqa.test;

import org.junit.Test;


public class GoogleTest extends TestBase {
  @Test
  public void userCanSearch() {
    onGooglePage()
    .searchFor("selenide")
    .ensureResultsContains("concise UI tests in Java")
    .ensureResultsHaveSize(10);
    
  }
}



// test/TestBase.java

package ua.in.externalqa.test;
import ua.in.externalqa.pages.GooglePage;
import static com.codeborne.selenide.Selenide.open;
import static com.codeborne.selenide.Configuration.baseUrl;

public class TestBase {
    
public GooglePage onGooglePage(){
baseUrl = "http://google.com";
GooglePage page = open("/ncr", GooglePage.class);
return page;
}

}



// pages/GooglePage.java

package ua.in.externalqa.pages;

import static com.codeborne.selenide.Selenide.$;
import static com.codeborne.selenide.Selenide.page;

public class GooglePage {
public SearchResultsPage searchFor(String text) {
        $("#lst-ib").val(text).pressEnter();
    return page(SearchResultsPage.class);
  }
}




// pages/SearchResultsPage.java

package ua.in.externalqa.pages;

import com.codeborne.selenide.ElementsCollection;
import static com.codeborne.selenide.CollectionCondition.size;
import static com.codeborne.selenide.Condition.visible;
import static com.codeborne.selenide.Selenide.$;
import static com.codeborne.selenide.Selenide.$$;

import org.openqa.selenium.By;

public class SearchResultsPage {
  
public SearchResultsPage ensureResultsContains(String text ) {
$(By.partialLinkText(text)).shouldBe(visible);
return this; 
}
public SearchResultsPage ensureResultsHaveSize(int size ) {
getResults().shouldHave(size(size));
return this; 
}
public ElementsCollection getResults() {
    return $$(By.className("rc"));
  }
}




No comments:

Post a Comment