From 9d49d03be09a06923051f5bc69deca30579cc1fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Comte?= Date: Sat, 2 Jan 2021 12:23:50 +0100 Subject: [PATCH] Fixing KW "Search in google" --- .../selenium/SeleniumKeywordExample.java | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/keywords/java/demo-selenium-keyword/src/main/java/step/examples/selenium/SeleniumKeywordExample.java b/keywords/java/demo-selenium-keyword/src/main/java/step/examples/selenium/SeleniumKeywordExample.java index 647a194..701689e 100644 --- a/keywords/java/demo-selenium-keyword/src/main/java/step/examples/selenium/SeleniumKeywordExample.java +++ b/keywords/java/demo-selenium-keyword/src/main/java/step/examples/selenium/SeleniumKeywordExample.java @@ -25,6 +25,7 @@ public class SeleniumKeywordExample extends AbstractKeyword { + private static final int IMPLICIT_WAIT = 30; final List defaultOptions = Arrays.asList(new String[] { "disable-infobars", "ignore-certificate-errors" }); final List headlessOptions = Arrays.asList(new String[] { "headless", "disable-gpu", "disable-sotfware-rasterizer" }); @@ -43,8 +44,8 @@ public void Open_chrome_new() { final WebDriver driver = new ChromeDriver(options); - driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); - driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS); + setImplicitWait(driver, IMPLICIT_WAIT); + driver.manage().timeouts().pageLoadTimeout(IMPLICIT_WAIT, TimeUnit.SECONDS); if (input.getBoolean("maximize", false)) { driver.manage().window().maximize(); @@ -55,6 +56,10 @@ public void Open_chrome_new() { setDriver(driver); } + private void setImplicitWait(final WebDriver driver, long implicitWaitIsSec) { + driver.manage().timeouts().implicitlyWait(implicitWaitIsSec, TimeUnit.SECONDS); + } + private static final String INPUT_SEARCH = "search"; @Keyword(name = "Search in google", schema = "{\"properties\":{\""+INPUT_SEARCH+"\":{\"type\":\"string\"}}, \"required\":[\""+INPUT_SEARCH+"\"]}") @@ -70,17 +75,20 @@ public void searchInGoogle() throws Exception { searchInput.sendKeys(searchString + Keys.ENTER); - WebElement resultCountDiv = driver.findElement(By.xpath("//div/nobr")); + driver.findElement(By.xpath("//div/nobr")); + setImplicitWait(driver, 0); List elements = driver.findElements(By.xpath("//div[@id='cnsw']/iframe")); + setImplicitWait(driver, IMPLICIT_WAIT); if(elements.size()>0) { driver.switchTo().frame(elements.get(0)); - driver.findElement(By.xpath("//div[@id='introAgreeButton']")).click(); - new WebDriverWait(driver, 10).until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//*[@id='cnsw']/iframe"))); + WebElement button = driver.findElement(By.xpath("//div[@id='introAgreeButton']")); + button.click(); + new WebDriverWait(driver, 10).until(ExpectedConditions.invisibilityOfAllElements(button)); driver.switchTo().defaultContent(); } - List resultHeaders = driver.findElements(By.xpath("//div[@class='r']//h3")); + List resultHeaders = driver.findElements(By.xpath("//div[@class='rc']//h3")); for (WebElement result : resultHeaders) { output.add(result.getText(), result.findElement(By.xpath("..//cite")).getText()); }