Если кратко, весь шаблон PageObject сводится к применению инкапсуляции ради сокрытия технических деталей шагов в тестах. Ниже представлены всего лишь несколько примеров простого теста с использованием инкапсуляции и без.
Copy public class GoogleTest {
@ BeforeClass
public static void setup () {
Configuration . baseUrl = "http://google.com/ncr"
}
@ Test
public void userCanSearch () {
open( "/" ) ;
$( By . name( "q" )) . val ( "selenide" ) . pressEnter ();
$$( ".srg .g" ) . shouldHave ( size( 10 ) );
$$( ".srg .g" ) . get ( 1 ) . shouldHave ( text( "Selenide: concise UI tests in Java" ) );
}
}
Copy public class GoogleTest {
@ BeforeClass
public static void setup () {
Configuration . baseUrl = "http://google.com/ncr"
}
@ Test
public void userCanSearch () {
GooglePage google = new GooglePage() ;
google . open () . searchFor ( "selenide" );
google . results () . shouldHave ( size( 10 ) );
google . results () . get ( 0 ) . shouldHave ( text( "Selenide: concise UI tests in Java" ) );
}
}
Copy public class GooglePage {
public GooglePage open () {
Selenide . open ( "/" );
return this ;
}
public GooglePage searchFor ( String text) {
$( By . name( "q" )) . val (text) . pressEnter ();
return this ;
}
public ElementsCollection results () {
return $$( ".srg .g" ) ;
}
}
Higher level encapsulation
Copy public class GoogleTest {
GooglePage google = new GooglePage() ;
SearchResults results = new SearchResults() ;
@ BeforeClass
public static void setup () {
Configuration . baseUrl = "http://google.com/ncr"
}
@ Test
public void userCanSearch () {
GooglePage google = new GooglePage() ;
google . open () . searchFor ( "selenide" );
results . shouldHaveSize ( 10 )
. shouldHaveResult ( 0 , "Selenide: concise UI tests in Java" );
}
}
Copy public class GooglePage {
public GooglePage open () {
Selenide . open ( "/" );
return this ;
}
public SearchResults searchFor ( String text) {
$( By . name( "q" )) . val (text) . pressEnter ();
return new SearchResults() ;
}
}
Copy public class SearchResults {
private final ElementsCollection elements = $$( ".srg .g" ) ;
public SearchResults shouldHaveSize ( int size) {
elements . shouldHaveSize (size);
return this ;
}
public SearchResults shouldHaveResult ( int index , String text) {
elements . shouldHave ( text(text) );
return this ;
}
}
Больше примеров и вариаций реализации шаблона PageObject можно найти в следующих докладах: