4

Actually I am working on one script in my application there I need to wait till its complete the execution, once complete the execute it will change the status. But I don't know when the execution completes (Dynamic time).

This is web-based application and I am not sure which wait or feature or method I need to use to wait until it satisfy the condition. Below is the sample code I tried (Can not post exact code).

Please suggest me how to proceed with or any suggestion on this.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.FluentWait;
import com.google.common.base.Function;
public class TestingClass {
 public static void main(String[] args) throws InterruptedException {
 System.setProperty("webdriver.gecko.driver", "D:\\SeleniumClass\\src\\libs\\geckodriver.exe");
 WebDriver driver = new FirefoxDriver();
 driver.get("https://toolsqa.com/automation-practice-switch-windows/");
 
 FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver);
 wait.pollingEvery(Duration.ofHours(5));
 wait.withTimeout(Duration.ofMinutes(1));
 
 Function<WebDriver, Boolean> function = new Function<WebDriver, Boolean>()
 {
 public Boolean apply(WebDriver arg0) {
 WebElement element = arg0.findElement(By.id("tabButton"));
 String type = element.getAttribute("type");
 System.out.println("The type is " + type);
 if(type.equals("button"))
 {
 return true;
 }
 return false;
 }
 };
 
 wait.until(function);
 }
}
Sachin
5105 silver badges18 bronze badges
asked Apr 22, 2021 at 12:53
3
  • 3
    Let us know how it went after 5 hours. :D Commented Apr 22, 2021 at 13:15
  • 2
    @MateMrše: Will see if anyone reply. Commented Apr 22, 2021 at 14:35
  • Upvote for this @Selenium Commented Apr 22, 2021 at 14:51

2 Answers 2

1

The Selenium Webdriver has all kinds of timeouts, waiting for 5 hours isnt going to work like this. I might try something like:

  1. Start browser with WebDriver and do some actions
  2. Quit browsers
  3. Create a while loop that waits a while (e.g. sleep a couple of minutes), then starts the a NEW browser, check if done, if not quit browser and repeat wait.

e.g. dont use Selenium todo the waiting, but your code and keep building a new session and use it only for a short while

answered Apr 22, 2021 at 15:29
1

Explicit wait is already available for most cases:

WebDriverWait explicit = new WebDriverWait(driver,Duration.ofHours(5));
explicit.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(text(),'COMPOSE')]")));

you can use expected conditions in that :

https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/support/ui/ExpectedConditions.html

if you want to use special condition then use :

package test_suites;
import static org.testng.Assert.assertTrue;
import java.io.IOException;
import java.time.Duration;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeOptions;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import Utils.excelParcer;
import driversetup.TestBaseClass;
import page_objects.homepage;
public class Testcase1 extends TestBaseClass {
 homepage homepage;
 WebDriver driver;
 @Test(description = "Test case 1 assert True")
 public void loginPageTitleTest2() {
 EdgeOptions options = new EdgeOptions();
 options.addArguments("--no-sandbox");
 // options.setBinary("C:\\Program Files
 // (x86)\\Microsoft\\Edge\\Application\\msedge.exe");
 options.addArguments("--disable-dev-shm-usage"); // overcome limited resource problems
 options.addArguments("disable-infobars"); // disabling infobars
 options.addArguments("--disable-extensions"); // disabling extensions
 options.addArguments("--disable-gpu"); // applicable to windows os only
 options.addArguments("--disable-dev-shm-usage");
 System.setProperty("webdriver.edge.driver", "C:\\Users\\prave\\Downloads\\msedgedriver.exe");
 driver = new EdgeDriver(options);
 FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver);
 wait.pollingEvery(Duration.ofHours(5));
 wait.withTimeout(Duration.ofMinutes(1));
 WebDriverWait explicit = new WebDriverWait(driver, Duration.ofHours(5));
 explicit.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(text(),'COMPOSE')]")));
 wait.until(this.attributeContains(By.xpath("//button"), "type", "button"));
 }
 @AfterMethod
 public void tearDown() {
 driver.quit();
 }
 
 static ExpectedCondition<Boolean> attributeContains(final By locator, final String attribute, final String value) {
 return new ExpectedCondition<Boolean>() {
 private String currentValue = null;
 @Override
 public Boolean apply(WebDriver driver) {
 return driver.findElement(locator).getAttribute(attribute) == value;
 }
 @Override
 public String toString() {
 return String.format("value found by %s to contain \"%s\". Current value: \"%s\"", locator, value,
 currentValue);
 }
 };
 }
 
}

Note: attributeContains is already available in selenium expected condition the code is just to show you how to create your on fluent wait and expected condition

answered Apr 23, 2021 at 3:50

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.