Артём Кошелев "Чего не хватает в PageObject?"

1,236 views

Published on

Если вам приходилось писать тесты для веб-интерфейсов, вы наверняка знакомы с паттерном проектирования PageObject. Несмотря на популярность и широкое распространение, он имеет серьезные недостатки. В докладе обсуждаются эти недостатки и предлагаются решения озвученных проблем.

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,236
On SlideShare
0
From Embeds
0
Number of Embeds
684
Actions
Shares
0
Downloads
8
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Артём Кошелев "Чего не хватает в PageObject?"

  1. 1. Чего не хватаетв PageObject?Кошелев АртёмРуководитель группы тестированияЯ.Субботник в Санкт-Петербурге, 1 декабря 2012
  2. 2. 2
  3. 3. h"ps://github.com/yandex-­‐qatools/htmlelements  3
  4. 4. 4
  5. 5. 5
  6. 6. 6
  7. 7. 7
  8. 8. 8
  9. 9. public class HomePageTest {! !@Test! !public void testSearchResults() {! ! !WebDriver driver = new FirefoxDriver();! ! ! !driver.get(“http://market.yandex.ru/”);! ! !driver.findElement(By.css(“input#search-input”))! ! ! !.sendKeys(“чикипибарум”);! ! !driver.findElement(By.css(“td.b-search__button”))! ! ! !.click();! ! ! !assertThat(“Не найдено что ничего не найдено”,! ! !driver.findElement(By.css(“p.no-found”)).getText(),! ! !is(“Заданным условиям не соответствует ни один товар”));! !}!9
  10. 10. public class HomePageTest {! !@Test! !public void testSearchResults() {! ! !WebDriver driver = new FirefoxDriver();! ! ! !driver.get(“http://market.yandex.ru/”);! ! !driver.findElement(By.css(“input#search-input”))! ! ! !.sendKeys(“чикипибарум”);! ! !driver.findElement(By.css(“td.b-search__button”))! ! ! !.click();! ! ! !assertThat(“Не найдено что ничего не найдено”,! ! !driver.findElement(By.css(“p.no-found”)).getText(),! ! !is(“Заданным условиям не соответствует ни один товар”));! !}!10
  11. 11. public class HomePageTest {! !@Test! !public void testSearchResults() {! ! !WebDriver driver = new FirefoxDriver();! ! ! !driver.get(“http://market.yandex.ru/”);! ! !driver.findElement(By.css(“input#search-input”))! ! ! !.sendKeys(“чикипибарум”);! ! !driver.findElement(By.css(“td.b-search__button”))! ! ! !.click();! ! ! !assertThat(“Не найдено что ничего не найдено”,! ! !driver.findElement(By.css(“p.no-found”)).getText(),! ! !is(“Заданным условиям не соответствует ни один товар”));! !}!11
  12. 12. public class HomePageTest {! !@Test! !public void testSearchResults() {! ! !WebDriver driver = new FirefoxDriver();! ! ! !driver.get(“http://market.yandex.ru/”);! ! !driver.findElement(By.css(“input#search-input”))! ! ! !.sendKeys(“чикипибарум”);! ! !driver.findElement(By.css(“td.b-search__button”))! ! ! !.click();! ! ! !assertThat(“Не найдено что ничего не найдено”,! ! !driver.findElement(By.css(“p.no-found”)).getText(),! ! !is(“Заданным условиям не соответствует ни один товар”));! !}!12
  13. 13. public class HomePageTest {! !@Test! !public void testSearchResults() {! ! !WebDriver driver = new FirefoxDriver();! ! ! !driver.get(“http://market.yandex.ru/”);! ! !driver.findElement(By.css(“input#search-input”))! ! ! !.sendKeys(“чикипибарум”);! ! !driver.findElement(By.css(“td.b-search__button”))! ! ! !.click();! ! ! !assertThat(“Не найдено что ничего не найдено”,! ! !driver.findElement(By.css(“p.no-found”)).getText(),! ! !is(“Заданным условиям не соответствует ни один товар”));! !}!13
  14. 14. public class HomePageTest {! !@Test! !public void testSearchResults() {! ! !WebDriver driver = new FirefoxDriver();! ! ! !driver.get(“http://market.yandex.ru/”);! ! !driver.findElement(By.css(“input#search-input”))! ! ! !.sendKeys(“чикипибарум”);! ! !driver.findElement(By.css(“td.b-search__button”))! ! ! !.click();! ! ! !assertThat(“Не найдено что ничего не найдено”,! ! !driver.findElement(By.css(“p.no-found”)).getText(),! ! !is(“Заданным условиям не соответствует ни один товар”));! !}!14
  15. 15. 15
  16. 16. 16
  17. 17. 17
  18. 18. 18
  19. 19. 19
  20. 20. 20
  21. 21. public class HomePage extends PageObject {! !private WebDriver driver;! ! !@FindBy(css = “input#search-input”)! !private WebElement searchInput;! ! !@FindBy(css = “td.b-search__button”)! !private WebElement searchButton;! ! !public HomePage(WebDriver driver) {! ! !return PageFactory.initElements(driver, this.class);! !}! ……!21
  22. 22. public class HomePage extends PageObject {! !private WebDriver driver;! ! !@FindBy(css = “input#search-input”)! !private WebElement searchInput;! ! !@FindBy(css = “td.b-search__button”)! !private WebElement searchButton;! ! !public HomePage(WebDriver driver) {! ! !return PageFactory.initElements(driver, this.class);! !}! ……!22
  23. 23. public class HomePage extends PageObject {! !private WebDriver driver;! ! !@FindBy(css = “input#search-input”)! !private WebElement searchInput;! ! !@FindBy(css = “td.b-search__button”)! !private WebElement searchButton;! ! !public HomePage(WebDriver driver) {! ! !return PageFactory.initElements(driver, this.class);! !}! ……!23
  24. 24. public class HomePage extends PageObject {! !private WebDriver driver;! ! !@FindBy(css = “input#search-input”)! !private WebElement searchInput;! ! !@FindBy(css = “td.b-search__button”)! !private WebElement searchButton;! ! !public HomePage(WebDriver driver) {! ! !return PageFactory.initElements(driver, this.class);! !}! ……!24
  25. 25. public class HomePage extends PageObject {! !……! ! !public SearchResultsPage lookFor(String item) {! ! !searchInput.type(item);! ! !searchButton.click();! ! !return new SearchResultsPage(driver);! !}! ! !public CatalogModelsPage clickOnElectronicLink() {! ! !electronikLink.click();! ! !return new CatalogModelsPage(driver);! !}! ! …… !!25
  26. 26. public class HomePage extends PageObject {! !……! ! !public SearchResultsPage lookFor(String item) {! ! !searchInput.type(item);! ! !searchButton.click();! ! !return new SearchResultsPage(driver);! !}! ! !public CatalogModelsPage clickOnElectronicLink() {! ! !electronikLink.click();! ! !return new CatalogModelsPage(driver);! !}! ! …… !!26
  27. 27. public class HomePage extends PageObject {! !……! ! !public SearchResultsPage lookFor(String item) {! ! !searchInput.type(item);! ! !searchButton.click();! ! !return new SearchResultsPage(driver);! !}! ! !public CatalogModelsPage clickOnElectronicLink() {! ! !electronikLink.click();! ! !return new CatalogModelsPage(driver);! !}! ! …… !!27
  28. 28. HomePage onHomePage;! SearchResultsPage onResultsPage;! ! @Test! public void canSearch() {! !onHomePage = new HomePage(driver)! ! ! ! ! !.open();! !onResultsPage = onHomePage! ! ! ! ! ! !.lookFor(“чикипибарум”);! ! !assertThat(onResultsPage.errorMessage(), ! ! !is(“Заданным условиям не соответствует ни один товар.”));! }!28
  29. 29. HomePage onHomePage;! SearchResultsPage onResultsPage;! ! @Test! public void canSearch() {! !onHomePage = new HomePage(driver)! ! ! ! ! !.open();! !onResultsPage = onHomePage! ! ! ! ! ! !.lookFor(“чикипибарум”);! ! !assertThat(onResultsPage.errorMessage(), ! ! !is(“Заданным условиям не соответствует ни один товар.”));! }!29
  30. 30. HomePage onHomePage;! SearchResultsPage onResultsPage;! ! @Test! public void canSearch() {! !onHomePage = new HomePage(driver)! ! ! ! ! !.open();! !onResultsPage = onHomePage! ! ! ! ! ! !.lookFor(“чикипибарум”);! ! !assertThat(onResultsPage.errorMessage(), ! ! !is(“Заданным условиям не соответствует ни один товар.”));! }!30
  31. 31. 31
  32. 32. 32
  33. 33. 33
  34. 34. public class HomePage extends PageObject {! ! !@FindBy(css = “input#search-input”)! !private WebElement searchInput;! ! !@FindBy(css = “td.b-search__button”)! !private WebElement searchButton;! !34
  35. 35. 35
  36. 36. 36
  37. 37. 37
  38. 38. 38
  39. 39. 39
  40. 40. 40
  41. 41. 41
  42. 42. 42
  43. 43. 43
  44. 44. 44
  45. 45. 45
  46. 46. 46
  47. 47. 47
  48. 48. 48
  49. 49. 49
  50. 50. 50
  51. 51. @Block(@FindBy(…))! public class SearchArrow extends HtmlElement {! ! !@FindBy(…)! ! !private WebElement searchInput;! ! ! !@FindBy(…)! ! !private WebElement searchButton;! }!51
  52. 52. @Block(@FindBy(…))! public class SearchArrow extends HtmlElement {! ! !@FindBy(…)! ! !private WebElement searchInput;! ! ! !@FindBy(…)! ! !private WebElement searchButton;! }!52
  53. 53. @Block(@FindBy(…))! public class SearchArrow extends HtmlElement {! ! !@FindBy(…)! ! !private WebElement searchInput;! ! ! !@FindBy(…)! ! !private WebElement searchButton;! }!53
  54. 54. @Block(@FindBy(…))! public class SearchArrow extends HtmlElement {! ! !@FindBy(…)! ! !private WebElement searchInput;! ! ! !@FindBy(…)! ! !private WebElement searchButton;! }!54
  55. 55. 55
  56. 56. @Block(@FindBy(…))! public class SearchBlock extends HtmlElement {! ! ! !private SearchArrow arrow;! ! !private SearchTabs tabs;! ! }!56
  57. 57. public class HomePage extends PageObject {! ! ! !public SearchArrow searchArrow;! ! }!57
  58. 58. public class HomePage extends PageObject {! ! !@FindBy(xpath = “//different_locator”)! !public SearchArrow searchArrow;! ! }!58
  59. 59. @Block(@FindBy(…))! public class SearchArrow extends HtmlElement {! ! !@FindBy(…)! ! !private WebElement searchInput;! ! ! !@FindBy(…)! ! !private WebElement searchButton;! }!59
  60. 60. 60
  61. 61. @Block(@FindBy(…))! public class SearchArrow extends HtmlElement {! ! !@FindBy(…)! ! !private TextInput searchInput;! ! ! !@FindBy(…)! ! !private Button searchButton;! }!61
  62. 62. 62
  63. 63. public class Link extends TypifiedElement {! ! !public Link(WebElement wrappedElement) {! ! !super(wrappedElement)! !}! ! }!63
  64. 64. public class Link extends TypifiedElement {! ! !……! ! !public void click() {…}! ! !public String getReferecnce() {…}! ! !public String getText(){…}! ! ! }!64
  65. 65. 65
  66. 66. 66
  67. 67. public class Range extends TypifiedElement {! ! !public Range(WebElement wrappedElement) {! ! !super(wrappedElement)! !}! ! }!67
  68. 68. public class Range extends TypifiedElement {! !@FindBy(…)! !WebElement min;! !@FindBy(…)! !WebElement max;! !! !public setRange(int min, int max) {…}! ! !public setMin(int min) {…}! ! public setMax(int max) {…}! }!68
  69. 69. 69
  70. 70. 70
  71. 71. 71
  72. 72. 72
  73. 73. 73
  74. 74. public class HomePage extends PageObject {! …! ! !@FindBy(css = “div.suggest”)! !private WebElement suggest;! ! …! }!74
  75. 75. 75
  76. 76. @Block(@FindBy(xpath = “div[@class*=‘search__arr’]”))! @Name(“Поисковая стрела”)! public class SearchArrow extends HtmlElement {! ! !@FindBy(xpath = “.//input”)! ! !@Name(“Поисковый запрос”)! ! !private TextInput searchInput;! ! ! !@FindBy(xpath = “.//input[@type=‘submit’]”)! ! !private Button searchButton;! }!76
  77. 77. @Block(@FindBy(xpath = “div[@class*=‘search__arr’]”))! @Name(“Поисковая стрела”)! public class SearchArrow extends HtmlElement {! ! !@FindBy(xpath = “.//input”)! ! !@Name(“Поисковый запрос”)! ! !private TextInput searchInput;! ! ! !@FindBy(xpath = “.//input[@type=‘submit’]”)! ! !private Button searchButton;! }!77
  78. 78. 78
  79. 79. assertThat(actual, Matcher);!79
  80. 80. assertThat(str, is(“Everybody dance now!”));! ! assertThat(year, lessThan(1999));! ! assertThat(year, both(lessThan(1999)).! ! ! ! ! !and(greaterThan(1990)));!80
  81. 81. 81
  82. 82. @FindBy(…)! private WebElement firstSuggest;! ! assertThat(firstSuggest, hasText(“canon”));!82
  83. 83. @FindBy(…)! private WebElement firstSuggest;! ! assertThat(firstSuggest, hasText(“canon”));!83
  84. 84. @FindBy(…)! private List<WebElement> suggest;! ! assertThat(suggest, hasItem(hasText(“canon”)));!84
  85. 85. 85
  86. 86. <dependency>! <groupId>ru.yandex.qatools.htmlelements</groupId>! <artifactId>htmlelements-java</artifactId>! <version>1.8</version>! ! </dependency>!86
  87. 87. 87
  88. 88. 88
  89. 89. 89
  90. 90. 90
  91. 91. 91
  92. 92. 92
  93. 93. h"ps://github.com/yandex-­‐qatools/htmlelements  93
  94. 94. Кошелев АртёмРуководитель группытестированияartkoshelev@yandex-team.ruart_koshelev@

×