Html elements. How to cook tasty tests

Download Html elements. How to cook tasty tests

Post on 21-Jun-2015

1.209 views

Category:

Technology

2 download

Embed Size (px)

DESCRIPTION

Source Code: https://github.com/yandex-qatools/htmlelements HtmlElements: This framework is designed to provide easy-to-use way of interaction with web-page elements in your tests. It may be considered as an extension of WebDriver Page Object. With the help of Html Elements framework you can group web-page elements into blocks, encapsulate logic of interaction with them and then easily use created blocks in page objects. It also provides a set of helpful matchers to use with web-page elements and blocks. See JavaDocs for more details.

TRANSCRIPT

<ul><li> 1. HtmlElements</li></ul><p> 2. PageObjects 3. StepsPageObjects 4. Tests StepsPageObjects 5. 6. 7. PageObjects 8. public class SearchPage {@FindBy(id = "text")private WebElement requestInput;@FindBy(xpath = "//input[@type=submit]")private WebElement searchButton;@FindBy(name = "login")private WebElement loginInput;@FindBy(name = "passwd")private WebElement passwordInput;@FindBy(name = "b-form-button__input")private WebElement authorizeButton;public SearchPage(WebDriver driver) {PageFactory.initElements(driver, this);}public void searchFor(String request) {requestInput.sendKeys(request);searchButton.click();}public void authorize (String login, String password) {loginInput.sendKeys(login);passwordInput.sendKeys(login);authorizeButton.click();}} 9. Steps 10. public class CommonUserSteps {public CommonUserSteps(WebDriver driver) {...}public SearchPage onSearchPage() {return searchPage;}public void loginAsCommonUser() {User user = UserFactory.getCommonUser();onSearchPage().authorize(user.getLogin, user.getPassword);}public void searchFor(String request) {onSearchPage().searchFor(request);}public void shouldSeeSearchResults() {assertTrue(onSearchPage().isSearchResultsBlockDisplayed());}} 11. Tests 12. public class SearchResultsTest {private WebDriver driver = new FirefoxDriver();private CommonUserSteps user = new CommonUserSteps(driver);@Beforepublic void loadPage() {driver.get("http://www.yandex.ru");}@Testpublic void shouldSeeSearchResultsWhenLookingUp() {user.searchFor("CodeFest");user.shouldSeeSearchResults();}@Afterpublic void closeDriver() {driver.quit();}} 13. Tests StepsPageObjects 14. Tests StepsPageObjects 15. 16. Tests StepsPageObjects 17. 18. 19. 20. 21. 22. public class AutoHomePage {@FindBy(name = "text")private WebElement requestInput;@FindBy(xpath = "//input[@type=submit]")private WebElement searchButton;@FindBy(name = "login")private WebElement loginInput;@FindBy(name = "passwd")private WebElement passwordInput;@FindBy(xpath = "//div[@class=b-domik__button]//input")private WebElement loginButton;@FindBy(name = "twoweeks")private WebElement rememberMeCheckBox;@FindBy(name = "text") 23. public class AutoHomePage {@FindBy(name = "text")private WebElement requestInput;@FindBy(xpath = "//input[@type=submit]")private WebElement searchButton;@FindBy(name = "login")private WebElement loginInput;@FindBy(name = "passwd")private WebElement passwordInput;@FindBy(xpath = "//div[@class=b-domik__button]//input")private WebElement loginButton;@FindBy(name = "twoweeks")private WebElement rememberMeCheckBox;@FindBy(name = "text")private WebElement requestInput;@FindBy(xpath = "//input[@type=submit]")private WebElement searchButton;@FindBy(name = "login")private WebElement loginInput;@FindBy(name = "passwd")private WebElement passwordInput;@FindBy(xpath = "//div[@class=b-domik__button]//input")private WebElement loginButton;@FindBy(name = "text")private WebElement requestInput;@FindBy(xpath = "//input[@type=submit]")private WebElement searchButton;@FindBy(name = "login")private WebElement loginInput;@FindBy(name = "passwd")private WebElement passwordInput;@FindBy(xpath = "//div[@class=b-domik__button]//input")private WebElement loginButton; 24. public class AutoHomePage {@FindBy(name = "text")private WebElement requestInput;@FindBy(xpath = "//input[@type=submit]")private WebElement searchButton;@FindBy(name = "login")private WebElement loginInput;@FindBy(name = "passwd")private WebElement passwordInput;@FindBy(xpath = "//div[@class=b-domik__button]//input")private WebElement loginButton;@FindBy(name = "twoweeks")private WebElement rememberMeCheckBox;@FindBy(name = "text")private WebElement requestInput;@FindBy(xpath = "//input[@type=submit]")private WebElement searchButton;@FindBy(name = "login")private WebElement loginInput;@FindBy(name = "passwd")private WebElement passwordInput;@FindBy(xpath = "//div[@class=b-domik__button]//input")private WebElement loginButton;@FindBy(name = "text")private WebElement requestInput;@FindBy(xpath = "//input[@type=submit]")private WebElement searchButton;@FindBy(name = "login")private WebElement loginInput;@FindBy(name = "passwd")private WebElement passwordInput;@FindBy(xpath = "//div[@class=b-domik__button]//input")private WebElement loginButton;@FindBy(name = "text")private WebElement requestInput;@FindBy(xpath = "//input[@type=submit]")private WebElement searchButton;@FindBy(name = "login")private WebElement loginInput;@FindBy(name = "passwd")private WebElement passwordInput;@FindBy(xpath = "//div[@class=b-domik__button]//input")private WebElement loginButton;@FindBy(name = "text")private WebElement requestInput;@FindBy(xpath = "//input[@type=submit]")private WebElement searchButton;@FindBy(name = "login")private WebElement loginInput;@FindBy(name = "passwd")private WebElement passwordInput;@FindBy(xpath = "//div[@class=b-domik__button]//input")private WebElement loginButton;@FindBy(name = "text")private WebElement requestInput;@FindBy(xpath = "//input[@type=submit]")private WebElement searchButton;@FindBy(name = "login")private WebElement loginInput;@FindBy(name = "passwd")private WebElement passwordInput;@FindBy(xpath = "//div[@class=b-domik__button]//input")private WebElement loginButton;@FindBy(name = "text")private WebElement requestInput;@FindBy(xpath = "//input[@type=submit]")private WebElement searchButton; 25. 26. 27. 28. 29. TestsStepsPageObject Element 30. PageObject Element 31. HtmlElements 32. 33. 34. PageObject Element 35. @Block(@FindBy(className = "b-head-search"))public class SearchArrow extends HtmlElement {@FindBy(id = "text")private WebElement requestInput;@FindBy(className = "b-form-button__input")private WebElement searchButton;public void searchFor(String request) {requestInput.sendKeys(request);searchButton.click();}} 36. @Block(@FindBy(className = "b-domik"))public class AuthorizationForm extends HtmlElement {@FindBy(name = "login")private WebElement loginInput;@FindBy(name = "passwd")private WebElement passwordInput;@FindBy(className = "b-form-button__input")private WebElement authorizeButton;public void authorize(String login, String pass) {loginInput.sendKeys(login);passwordInput.sendKeys(password);authorizeButton.click();}} 37. PageObject Element 38. public class SearchPage {@FindBy(id = "text")private WebElement requestInput;@FindBy(xpath = "//input[@type=submit]")private WebElement searchButton;@FindBy(name = "login")private WebElement loginInput;@FindBy(name = "passwd")private WebElement passwordInput;@FindBy(name = "b-form-button__input")private WebElement authorizeButton;public SearchPage(WebDriver driver) {PageFactory.initElements(driver, this);}...} 39. public class SearchPage {private SearchArrow searchArrow;private AuthorizationForm authorizationForm;public SearchPage(WebDriver driver) {HtmlElementLoader.populatePageObject(this, driver);}public void searchFor(String request) {searchArrow.searchFor(request);}public void login(String login, String password) {authorizationForm.login(login, password);}} 40. 41. public class AutoSearchPage {@FindBy(className = "b-search")private SearchArrow searchArrow;private SearchResultsBlock searchResultsBlock;public SearchResultsPage(WebDriver driver) {HtmlElementLoader.populatePageObject(this, driver);}public void searchFor(String request) {searchArrow.searchFor(request);}public boolean isSearchResultsBlockDisplayed() {return searchResultsBlock.isDisplayed();}} 42. public class SearchPage {@FindBy(className = "b-search")private SearchArrow searchArrow;}@Block(@FindBy(className = "b-head-search"))public class SearchArrow extends HtmlElement {...} 43. 44. 45. GodeFest 46. GodeFest 47. GodeFest@Block(@FindBy(className = "b-head-search"))public class SearchArrow extends HtmlElement {@FindBy(id = "text")private WebElement requestInput;@FindBy(className = "b-form-button__input")private WebElement searchButton;@FindBy(xpath = "//div[@class=i-popup__content]//li")private List suggest;public void searchBySuggest(String request, int suggestItemNumber) {requestInput.sendKeys(request);suggest.get(suggestItemNumber).click();}} 48. GodeFest@Block(@FindBy(className = "b-head-search"))public class SearchArrow extends HtmlElement {@FindBy(id = "text")private WebElementrequestInput;TextInput requestInput;@FindBy(className = "b-form-button__input")private WebElement searchButton;Button searchButton;@FindBy(className = "i-popup__content")@FindBy(xpath = "//div[@class=i-popup__content]//li")private List suggest;Suggest suggest;public void searchBySuggest(String request, int suggestItemNumber) {requestInput.sendKeys(request);suggest.selectByIndex(suggestItemNumber);suggest.get(suggestItemNumber).click();}} 49. TextBlockTextInput ButtonLink 50. Checkbox RadioForm Select Table 51. Checkboxpublic class Checkbox extends TypifiedElement {public WebElement getLabel(){...}public String getLabelText(){...}public String getText(){...}public void select(){...}public void deselect(){...}public void set(boolean checked){...}public boolean isSelected(){...}} 52. Radiopublic class Radio extends TypifiedElement {public List getButtons(){...}public WebElement getSelectedButton(){...}public void selectByValue(String value){...}public void selectByIndex(int index){...}} 53. public class Select extends TypifiedElement {public boolean isMultiple(){...} Selectpublic List getOptions(){...}public List getAllSelectedOptions(){...}public WebElement getFirstSelectedOption(){...}public void selectByVisibleText(String text){...}public void selectByIndex(int index){...}public void selectByValue(String value){...}public void deselectAll(){...}public void deselectByValue(String value){...}public void deselectByIndex(int index){...}public void deselectByVisibleText(String text){...}} 54. Formpublic class Form extends TypifiedElement {public void fill(Map data){...}public void fill(T bean, Converter Converter){...}protected WebElement findFiledByKey(String key){...}protected void fillField(WebElement e, Object value){...}public WebElement findFirstField(By by){...}public List findFields(By by){...}public List findSelectsBy(By by){...}} 55. Formpublic class SearchPage {@FindBy(className = "b-domik")private Form authorizationForm;}public class CommonUserSteps {public void loginAsCommonUser() {User user = UserFactory.getCommonUser();HashMap userHash = convert(user);onSearchPage().getAuthorizationForm.fill(userHash);}} 56. Tablepublic class Table extends TypifiedElement {public List getHeadings(){...}public List getHeadingsAsString(){...}public List&gt; getRows(){...}public List&gt; getColumns(){...}public WebElement getCellAt(int i, int j){...}public List getRowsMappedToHeadings() {...}public List getRowsMappedToHeadings(List h){...}public List getRowsAsStringMappedToHeadings(){...}public List getRowsAsStringMappedToHeadings(List h){...}} 57. Tablepublic class SomeSecretePage {@FindBy(className = "b-secret")private Table users;}public class SearchUserSteps {public List findAllUsers() {Table usersTable = onSomeSecretPage().getUsersTable();List hash = usersTable.getRowsMappedToHeadings();List users = convert(hash);return users;}} 58. Converters 59. GodeFest@Block(@FindBy(className = "b-head-search"))public class SearchArrow extends HtmlElement {@FindBy(id = "text")private TextInput requestInput;@FindBy(className = "b-form-button__input")private Button searchButton;@FindBy(className = "i-popup__content")private Suggest suggest;public void searchBySuggest(String request, int suggestItemNumber) {requestInput.sendKeys(request);suggest.selectByIndex(suggestItemNumber);}} 60. GodeFest@Block(@FindBy(className = "b-head-search"))public class SearchArrow extends HtmlElement {@FindBy(id = "text")private TextInput requestInput;@FindBy(className = "b-form-button__input")private Button searchButton;@FindBy(className = "i-popup__content")private Suggest suggest;public void searchBySuggest(String request, int suggestItemNumber) {requestInput.sendKeys(request);suggest.selectByIndex(suggestItemNumber);}} 61. public class Suggest extends TypifiedElement {public Suggest(WebElement wrappedElement) {super(wrappedElement);}private List getItems() {return getWrappedElement().findElements(By.xpath("//li"));}public void selectByIndex(int itemIndex) {getItems().get(itemIndex).click();}public void selectByValue(String itemValue) {for (WebElement item : getItems()) {if (itemValue.equals(item.getText())) {item.click();return;}}throw new NoSuchElementException();}} 62. 63. 64. 65. assertThat(audience, isPayingAttention()) 66. assertThat(audience, isPayingAttention())assertThat(audience, is(not(sleeping()))) 67. 68. assertThat(element, exists()) 69. assertThat(element, exists())assertThat(element, hasText(CodeFest)) 70. assertThat(element, exists())assertThat(element, hasText(CodeFest)) assertThat(checkBox, isSelected()) 71. assertThat(element, exists())assertThat(element, hasText(CodeFest)) assertThat(checkBox, isSelected())assertThat(radio, hasSelectedButton(3)) 72. public class CommonUserSteps {public void shouldSeeSearchResults() { assertThat(onSearchPage().getSearchResults(), exist() );}} 73. public class CommonUserSteps {public void shouldSeeSearchResults() {assertThat(onSearchPage().getSearchResults(),both(exist()).and(isDisplayed()));}} 74. public class CommonUserSteps {public void shouldSeeSearchResults(Matcher mch) {assertThat( onSearchPage().getSearchResults(), mch);}} 75. @Testpublic void shouldSeeSearchResultsWhenLookingUp() {user.searchFor("CodeFest");user.shouldSeeSearchResults(both(exists()).and(isDisplayed()));} 76. @Testpublic void shouldSeeSearchResultsWhenLookingUp() {user.searchFor("CodeFest");user.shouldSeeSearchResults(both(exists()).and(isDisplayed()));}Expected: element existing on page but: was ...]&gt; 77. @Block(@FindBy(className = "b-head-search"))public class SearchResultBlock extends HtmlElement {@Name(" ")@FindBy(id = "results")private List searchResults;@FindBy(id = "info")private TextBlock searchInfo;} 78. public class SearchPage {@Name( )private SearchResultsBlock searchResultsBlock;}@Block(@FindBy(className = "b-head-search"))public class SearchResultsBlock extends HtmlElement {...} 79. @Testpublic void shouldSeeSearchResultsWhenLookingUp() {user.searchFor("CodeFest");user.shouldSeeSearchResults(both(exists()).and(isDisplayed()));} Expected: element existing on pagebut: was SearchResultBlock 80. 81. Waiter Decorator@Testpublic void shouldSeeSearchResultsWhenLookingUp() {user.searchFor("CodeFest");user.shouldSeeSearchResults(should(exists()).whileWaitingUntil(timeoutHasExpired()));} Expected: while waiting for [30000] ms element existing on pagebut: was SearchResultBlock 82. Action Decorator@Testpublic void shouldSeeSearchResultsWhenLookingUp() {user.searchFor("CodeFes...</p>