Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Sleeping is not your best friend

461 views

Published on

Sometimes automation testers are caught red handed inserting sleeps in tricky places. Instead of giving in to frustration, patiently and persistently show them how to use intelligent waits for specific conditions.
One goal of this talk is to emphasise that we need to aim for efficient and reliable tests. No more flakey tests.

Published in: Software
  • Be the first to comment

Sleeping is not your best friend

  1. 1. Sleeping is your best friend in automation! NOT
  2. 2. Michelle Macdonald Pronto Software Limited Selenium Automation 5+ years later...
  3. 3. Selenium Appium
  4. 4. Stay Awake? http://theideas.wikia.com/wiki/SpongeBob_SquarePants_(character)
  5. 5. Though sleep is called our best friend, it is a friend who often keeps us waiting!~ Jules Verne
  6. 6. What sleeps for 20 hours a day?
  7. 7. Photo: Rachel Newton
  8. 8. What sleeps for 10 seconds before logging on?
  9. 9. 
 browser = webdriver.Firefox()
 browser.get(url) 
 time.sleep(10)
 user_name.send_keys("username")
 pass_word.send_keys("password")
 login_button.click()
 random script
  10. 10. 40 WIDE AWAKE MINUTES LATER ...
  11. 11. Pineapple Wisdom! http://theideas.wikia.com/wiki/SpongeBob_SquarePants_(character)
  12. 12. ✦ Sleep = Friend? ✦ Community ? ✦ Why? Only human! ✦ New Attitudes ✦ Convenient examples ✦ Sleep Challenges
  13. 13. Sleep Pause Timeout Delay Suspend
  14. 14. What does the selenium community say?
  15. 15. Sleeps don’t make our tests run as quickly as possible! https://github.com/AutomatedTester Burns
  16. 16. Dave Haeffner's Proven Method to Grading Selenium Tests Hard Coded Sleep? Deduct -5 points. https://i.ytimg.com/vi/NvGkclsL7rA/maxresdefault.jpg Haeffner
  17. 17. Thread.sleep is the case of an explicit wait https://www.seleniumhq.org/docs/04_webdriver_advanced.jsp#explicit-and-implicit-waits worst
  18. 18. SEVERAL SLEEP SLASHING MINUTES LATER...
  19. 19. Unreliable http://getdrawings.com/cliparts/squidward-clipart-29.png
  20. 20. Inefficient http://spongebob.wikia.com/wiki/File:Im_thmart.png
  21. 21. Inaccurate http://spongebob.wikia.com/wiki/File:Squidward_Tentacles_(1).png
  22. 22. 
 Thread.sleep(2000)
 EXTERMINATE! http://spongebob.wikia.com/wiki/File:Plankton_1.png
  23. 23. We are human after all ...
  24. 24. Why do we add sleeps? amazing artwork by mishmaccas
  25. 25. Our Goal: Selenium in Sync with Browser with No Sleeps!
  26. 26. I’m Ready! https://tenor.com/search/spongebob-im-ready-gifs
  27. 27. sELeNiUm ProVides!
  28. 28. Web driver wait WebDriverWait wait = new WebDriverWait(driver, 10); Alert al = wait.until( ExpectedConditions.alertIsPresent()); al.accept(); try { return driver.switchTo().alert(); } catch (NoAlertPresentException e)
  29. 29.  new FluentWait<WebDriver>(webDriver) .withTimeout(Duration.ofSeconds(max)) .pollingEvery(Duration.ofMillis(millis)) .ignoring(NoSuchElementException.class); Fluent wait
  30. 30. public boolean enabled(WebElement element,  int time) {     FluentWait<WebDriver> wait = fluentWait(time);     return (boolean) wait.until(new ExpectedCondition<Boolean>() {          public Boolean apply(WebDriver driver) {             return element.isEnabled(); Define Conditions assertTrue(waits.enabled(pageObject.element, timeout));
  31. 31. IDE waitForElement(locator, driver) { const elementLocator = parseLocator(locator); await driver.wait(until.elementLocated(elementLocator), IMPLICIT_WAIT);
  32. 32. VeRy ConVeniENt MetHoDs
  33. 33. Expected Conditions
  34. 34. “Example wait.until( ExpectedConditions.elementToBeClickable(locator)); Clickable Element is: • Not null • Visible • Not Disabled
  35. 35. Cool’ common conditions but...
  36. 36. Javascript > webdriver.io Ruby > Watir DotNet > WaitHelpers Other Languages
  37. 37. wait.until... webdriver.findElement(By.tagName(“textarea”)) .getAttribute(“value”).contains(value); Custom waits.textAreaContains(“Hello Bikini Bottom”, 5);
  38. 38. CoMmoN eveRydAy waiTs
  39. 39. Visibility
  40. 40. Present “Search” Input Element CssSelector [id=‘search-input’]
  41. 41. ExpectedConditions .attributeContains(element, “class”,"hidden") Present But ... Hidden ExpectedConditions .presenceOfElementLocatedBy(locator)
  42. 42. Visible Select “Year”
  43. 43. assertTrue(waits.isVisible(datePicker.selectYear)); ExpectedConditions.visibilityOf(element),
  44. 44. Invisible Note Pad Icon assertTrue(waits.invisibilityOf(locator, timeout)); ExpectedConditions. .invisibilityOfElementLocated(locator) CssSelector “img[src*=‘note’]"
  45. 45. Appium Samsung Tablet Select option Wait for Text Text
  46. 46. tapMe(nav.company(“BEA”)); SCA - South Campbell Co
  47. 47. wait.until(new ExpectedCondition<Boolean>() {      public Boolean apply(WebDriver driver) {        return element.getAttribute("textContent").contains(text) wait.textContains( nav.companyName, “BEA - Berry Company”, 5);
  48. 48. Value Appium iOS Simulator Sales App Increase Qty tapMe(sale.increaseQty);
  49. 49. fluentWait(ExpectedConditions .textToBePresentInElementValue(element, text, timeout) waitTextValue(sale.saleLineQuantity, “2”, 5);
  50. 50. Attribute Block Colour Style Change blue to safety orange
  51. 51. view.waitColourPicker(); view.safetyOrange().click(); Style contains “255, 105, 0”
  52. 52. Element view.working() Attribute “style” Colour 255, 105, 0 ExpectedConditions.attributeContains( element, attribute, colour), timeout)
  53. 53. Clickability
  54. 54. Enabled Context Menu Option assertTrue(waits.isEnabled(menu.copyToCursor, 3)); ExpectedConditions.elementToBeClickable(element, timeout)
  55. 55. Check Box Disabled waits.disabled( view.getCheckBox(“Phone / Fax Details”), 5); Disabled
  56. 56. “class” contains “disabled" OR “disabled” is TRUE OR “read-only” is TRUE
  57. 57. Radio Button Selected clickRadioButton("Stock/Addition Acquisition"); waitSelected( getRadioButton(“Stock/Addition Acquisition”));
  58. 58. wait.until(webDriver -> { return element.isSelected();
  59. 59. Focus Input Element
  60. 60. waitInputField(page.postcode); wait.until(webDriver -> { return webDriver.switchTo().activeElement().equals(element). && element.isDisplayed() && element.getAttribute("class").contains("waiting-field");
  61. 61. Toolkit Value Text Css Style Enabled Hidden Focus Visible Stale Alert Selected Disabled Present
  62. 62. ONE ETERNITY LATER...
  63. 63. MishMac SleepRants
  64. 64. The Invasion of the Upward Sliding Cards!
  65. 65. Open a card [program] Card slides down Cannot click whilst animating Episode 1
  66. 66. Smooth Slide
  67. 67. ONE PRODUCTIVE AGILE SPRINT LATER...
  68. 68. <div name="webclient/seleniumConf/mywindow" class="card active " Animation over <div name="CardContent" class=“card-content animationover"
  69. 69. //div[contains(@name, '" + name + "')] [contains(@class, ‘card')] [not(contains(@class, ‘inactive'))] [not(contains(@class, ‘collapsed'))] [contains(@class, ‘active')] /child::div[contains(@class, 'card-content animationover')] Welsh XPath
  70. 70. Parent div[name*=‘name'] [class*=‘active'] [class*=‘card']" Child div[name*='name'] > div[name=‘CardContent’] [class*='animationover']" Simple Css fluentWait(ExpectedConditions.and( ExpectedConditions.visibilityOfElementLocated(parent), ExpectedConditions.visibilityOfElementLocated(child), ExpectedConditions.numberOfElementsToBe(parent, 1)
  71. 71. getCard(program, timeout); One method
  72. 72. Find a Selenium Champion Today!
  73. 73. The mystery of the Un-clickable element!
  74. 74. Edit Window (form or modal) Click Save Window Exit Click on another element Episode 2 Element not clickable at point Hint! Other element would receive the click
  75. 75. Stale/ Invisible Window
  76. 76. 200 SUPERFAST MILLISECONDS LATER ...
  77. 77.  try{ fluentWait(ExpectedConditions .invisibilityOf(element), timeout);         return true; Go Away! edit.save().click(); // Wait Until NOT displayed assertTrue(super.waitInvisible(edit.contactForm, 2)); // Ready! page.element.click();
  78. 78. Wait for elements to be GONE
  79. 79. The misery and utter loathing of evil expanding charts!
  80. 80. Load sales widgets Click on bar graph chart ( to drill down ) Episode 3 Can’t click on expanding bars!
  81. 81. Spinners Gone ExpectedConditions.numberOfElementsToBe(locator, 0) CssSelector [class=‘wks-spinner’] Still Not Ready
  82. 82. CssSelector class=[‘div.chart c3 chart-animation’] Animation Gone ExpectedConditions.numberOfElementsToBe(locator, 0)), timeout); Still Not Ready
  83. 83. CssSelector [class='chart c3’] fluentWait(ExpectedConditions.and( ExpectedConditions .visibilityOfAllElementsLocatedBy(locator), ExpectedConditions .numberOfElementsToBe(locator, 6) ), timeout); 6 Charts Still Not Ready
  84. 84. ONE EMBARRASSING & SHAMEFUL YEAR LATER...
  85. 85. Click... When?
  86. 86. Xpath (//div[@id=‘id']//*[name()='svg'] //*[name()='g']/*[contains(@class, 'animated')])[3] What changed? Output for int i = 0; i < 15; i++){
 System.out.println( webDriver.findElement(locator).getAttribute(“class”));
 }
  87. 87. What?
  88. 88. Xpath [contains(@class, 'expanded')] Expanded fluentWait(ExpectedConditions .presenceOfAllElementsLocatedBy(locator), timeout;
  89. 89. Study element behaviours
  90. 90. The Terminal Sleep-Fest Mutiny!
  91. 91. Open Terminal Window Entering text command prompt How to wait for prompts? Episode 4
  92. 92. BOSS SHAMING
  93. 93. Thread.sleep(500); terminal.sendKeys("cd $HOME” + Keys.ENTER); Thread.sleep(500); terminal.sendKeys(“ls *.txt” + Keys.ENTER); Thread.sleep(500); terminal.sendKeys(“cp patrick.txt krustykrabs.txt” + Keys.ENTER); Thread.sleep(500);
  94. 94. prompt = "sh-4.1$ "; Prompt
  95. 95. Div 1 //div[@class=‘terminal’]/div[1] Div 2 //div[@class=‘terminal’]/div[2] Div 3 //div[@class=‘terminal’]/div[3] Divs
  96. 96. Method waitForPrompt("sh-4.1$ “, 1); Simple Wait ExpectedCondition textToBePresentInElementLocated(locator, text), timeout);
  97. 97. waitForPrompt("sh-4.1$ ", 1 );
 terminal.sendKeys("cd $HOME” + Keys.ENTER); waitForPrompt("sh-4.1$ ", 2 ); terminal.sendKeys(“ls *.txt” + Keys.ENTER);
  98. 98. Inspect = Simple Solution
  99. 99. A SPANNER THROWN IN THE WORKS
  100. 100. Quick Fix • Enter terminal commands • Capture screen shot • Convert greyscale image • Extract characters (OCR) • Assert expected content
  101. 101. 4 sleep destroying episodes later...
  102. 102. SEVERAL SLEEPY & JET LAGGED TRAVEL DAYS LATER...
  103. 103. http://spongebob.wikia.com/wiki/File:Plankton_1.png http://pngimg.com/imgs/animals/koala/ http://theideas.wikia.com/wiki/SpongeBob_SquarePants_(character) “I’m Ready!”
  104. 104. @mishmaccas mishmaccas@gmail.com twitter email

×