HtmlElements – естественное расширение PageObject

1,248 views
1,138 views

Published on

Доклад Артема Ерошенко и Александра Толмачева, SQA Days-13, 26-27 апреля 2013, www.sqadays.com

Published in: Education, Technology, Sports
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,248
On SlideShare
0
From Embeds
0
Number of Embeds
377
Actions
Shares
0
Downloads
15
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

HtmlElements – естественное расширение PageObject

  1. 1. HtmlElements
  2. 2. PageObjects
  3. 3. StepsPageObjects
  4. 4. StepsTestsPageObjects
  5. 5. Рецепт приготовления
  6. 6. Вкусных тестов
  7. 7. PageObjects
  8. 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. 9. Steps
  10. 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. 11. Tests
  12. 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("PageObject");user.shouldSeeSearchResults();}@Afterpublic void closeDriver() {driver.quit();}}
  13. 13. StepsTestsPageObjects
  14. 14. StepsTestsPageObjects
  15. 15. Модель устойчива
  16. 16. StepsTestsPageObjects
  17. 17. Шаблонизаторы
  18. 18. Шаблонизаторы
  19. 19. Шаблонизаторы
  20. 20. Загромождение
  21. 21. Загромождение
  22. 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. 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. 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. 25. Недостатки
  26. 26. Дублирование кодаНедостатки
  27. 27. Дублирование кодаЗагромождениеНедостатки
  28. 28. Коронноеблюдо
  29. 29. StepsTestsElementPageObject
  30. 30. ElementPageObject
  31. 31. HtmlElements
  32. 32. Блоки элементовТипизация элементовБиблиотека матчеров
  33. 33. Блоки элементовТипизация элементовБиблиотека матчеров
  34. 34. ElementPageObject
  35. 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. 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. 37. ElementPageObject
  38. 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. 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. 40. Структура страницы
  41. 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. 42. @Block(@FindBy(className = "b-head-search"))public class SearchArrow extends HtmlElement {...}Переопределениеpublic class SearchPage {@FindBy(className = "b-search")private SearchArrow searchArrow;}
  43. 43. Блоки элементовТипизация элементовБиблиотека матчеров
  44. 44. Типизация
  45. 45. в чем смыслТипизация
  46. 46. в чем смыслТипизация
  47. 47. Поисковая формав чем смысл@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<WebElement> suggest;public void searchBySuggest(String request, int suggestItemNumber) {requestInput.sendKeys(request);suggest.get(suggestItemNumber).click();}}
  48. 48. Поисковая формав чем смысл@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<WebElement> suggest;public void searchBySuggest(String request, int suggestItemNumber) {requestInput.sendKeys(request);suggest.get(suggestItemNumber).click();}}@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);}}
  49. 49. СтандартныеTextBlockTextInputLinkButtonFileInput
  50. 50. РасширенныеSelectRadioTableFormCheckbox
  51. 51. public 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(){...}}Checkbox
  52. 52. public class Radio extends TypifiedElement {public List<WebElement> getButtons(){...}public WebElement getSelectedButton(){...}public void selectByValue(String value){...}public void selectByIndex(int index){...}}Radio
  53. 53. public class Select extends TypifiedElement {public boolean isMultiple(){...}public List<WebElements> getOptions(){...}public List<WebElement> 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){...}}Select
  54. 54. Formpublic class Form extends TypifiedElement {public void fill(Map<String, Object> data){...}public <T> 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<WebElement> findFields(By by){...}public List<WebElement> findSelectsBy(By by){...}}
  55. 55. <form><some_style_tags><input name=”login” ...></some_style_tags><some_style_tags><input name=”passwd” ...></some_style_tags></form>public class User {@WebInput("login")public String login;@WebInput("passwd")public String password;}
  56. 56. public class SearchPage {@FindBy(className = "b-domik")private Form authorizationForm;}public class CommonUserSteps {public void loginAsCommonUser() {User user = UserFactory.getCommonUser();HashMap<String, Object> userHash = convert(user);onSearchPage().getAuthorizationForm.fill(userHash);}}public class CommonUserSteps {public void loginAsCommonUser() {User user = UserFactory.getCommonUser();HashMap<String, Object> userHash = convert(user);onSearchPage().getAuthorizationForm.fill(userHash);}}public class User {@WebInput("login")public String login;@WebInput("passwd")public String password;}
  57. 57. public class Table extends TypifiedElement {public List<WebElement> getHeadings(){...}public List<String> getHeadingsAsString(){...}public List<List<WebElement>> getRows(){...}public List<List<WebElement>> getColumns(){...}public WebElement getCellAt(int i, int j){...}public List<Map> getRowsMappedToHeadings() {...}public List<Map> getRowsMappedToHeadings(List<String> h){...}public List<Map> getRowsAsStringMappedToHeadings(){...}public List<Map> getRowsAsStringMappedToHeadings(List h){...}}Table
  58. 58. public class User {@WebInput("login")public String login;@WebInput("passwd")public String password;}Login Passwderoshenkoam c4fmUDTmbCzy8dautotester 7cRrTT7g
  59. 59. public class User {@WebInput("login")public String login;@WebInput("passwd")public String password;}public class SearchUserSteps {public List<User> findAllUsers() {Table usersTable = onSomeSecretPage().getUsersTable();List<Map> hash = usersTable.getRowsMappedToHeadings();List<User> users = convert(hash);return users;}}public class SomeSecretePage {@FindBy(className = "b-secret")private Table users;}
  60. 60. Converters
  61. 61. Поисковая формав чем смысл@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);}}
  62. 62. Поисковая формав чем смысл@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);}}
  63. 63. Добавление элементовpublic class Suggest extends TypifiedElement {public Suggest(WebElement wrappedElement) {super(wrappedElement);}private List<WebElement> 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();}}
  64. 64. КонструкторТипизированные элементыСтраница
  65. 65. Блоки элементовТипизация элементовБиблиотека матчеров
  66. 66. Матчеры
  67. 67. МатчерыassertThat(audience, isPayingAttention())
  68. 68. МатчерыassertThat(audience, isPayingAttention())assertThat(audience, is(not(sleeping())))
  69. 69. Матчеры элементов
  70. 70. assertThat(element, exists())Матчеры элементов
  71. 71. assertThat(element, exists())assertThat(element, hasText(“Yandex”))Матчеры элементов
  72. 72. assertThat(element, exists())assertThat(element, hasText(“Yandex”))assertThat(checkBox, isSelected())Матчеры элементов
  73. 73. assertThat(element, exists())assertThat(element, hasText(“Yandex”))assertThat(checkBox, isSelected())assertThat(radio, hasSelectedButton(3))Матчеры элементов
  74. 74. Блоки элементовТипизация элементовБиблиотека матчеров
  75. 75. Попробуйтесами
  76. 76. StepsTestsElementPageObject
  77. 77. HtmlElements
  78. 78. ТехнологииConverter = LambdaJStep FRWK = 2CDDsMatchers = Hamcrest
  79. 79. https://groups.google.com/forum/?fromgroups#!forum/yandex-qatools-htmlelementsqa-tools@yandex-team.ru@eroshenkoamhttps://github.com/yandex-qatools/htmlelements

×