Skip to content

Commit

Permalink
added global option to wait till an element is found
Browse files Browse the repository at this point in the history
Signed-off-by: aryangupta701 <[email protected]>
  • Loading branch information
aryangupta701 committed Aug 13, 2023
1 parent ec01709 commit f873361
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/main/java/org/zaproxy/zest/core/v1/ZestClientElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
package org.zaproxy.zest.core.v1;

import java.time.Duration;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

/** An abstract class representing an action on a client element. */
public abstract class ZestClientElement extends ZestClient {
Expand Down Expand Up @@ -58,7 +61,40 @@ protected WebElement getWebElement(ZestRuntime runtime) throws ZestClientFailExc
this, "No client: " + runtime.getVariable(getWindowHandle()));
}
String elem = runtime.replaceVariablesInString(this.getElement(), false);
String waitForElementToAppear = runtime.getVariable("waitForElementToAppear");

if (waitForElementToAppear != null && waitForElementToAppear.equals("true")) {
WebDriverWait wait = new WebDriverWait(wd, Duration.ofSeconds(30));
try {
if ("className".equalsIgnoreCase(type)) {
return wait.until(
ExpectedConditions.visibilityOfElementLocated(By.className(elem)));
} else if ("cssSelector".equalsIgnoreCase(type)) {
return wait.until(
ExpectedConditions.visibilityOfElementLocated(By.cssSelector(elem)));
} else if ("id".equalsIgnoreCase(type)) {
return wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(elem)));
} else if ("linkText".equalsIgnoreCase(type)) {
return wait.until(
ExpectedConditions.visibilityOfElementLocated(By.linkText(elem)));
} else if ("name".equalsIgnoreCase(type)) {
return wait.until(ExpectedConditions.visibilityOfElementLocated(By.name(elem)));
} else if ("partialLinkText".equalsIgnoreCase(type)) {
return wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.partialLinkText(elem)));
} else if ("tagName".equalsIgnoreCase(type)) {
return wait.until(
ExpectedConditions.visibilityOfElementLocated(By.tagName(elem)));
} else if ("xpath".equalsIgnoreCase(type)) {
return wait.until(
ExpectedConditions.visibilityOfElementLocated(By.xpath(elem)));
}
throw new ZestClientFailException(this, "Unsupported type: " + type);
} catch (Exception e) {
throw new ZestClientFailException(this, e);
}
}
try {
if ("className".equalsIgnoreCase(type)) {
return wd.findElement(By.className(elem));
Expand Down

0 comments on commit f873361

Please sign in to comment.