Skip to content

Commit

Permalink
Fixing KW "Search in google"
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromecomte committed Jan 2, 2021
1 parent 42a959d commit 9d49d03
Showing 1 changed file with 14 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

public class SeleniumKeywordExample extends AbstractKeyword {

private static final int IMPLICIT_WAIT = 30;
final List<String> defaultOptions = Arrays.asList(new String[] { "disable-infobars", "ignore-certificate-errors" });
final List<String> headlessOptions = Arrays.asList(new String[] { "headless", "disable-gpu", "disable-sotfware-rasterizer" });

Expand All @@ -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();
Expand All @@ -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+"\"]}")
Expand All @@ -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<WebElement> 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<WebElement> resultHeaders = driver.findElements(By.xpath("//div[@class='r']//h3"));
List<WebElement> resultHeaders = driver.findElements(By.xpath("//div[@class='rc']//h3"));
for (WebElement result : resultHeaders) {
output.add(result.getText(), result.findElement(By.xpath("..//cite")).getText());
}
Expand Down

0 comments on commit 9d49d03

Please sign in to comment.