We are having intermittent failures on the waits, although we have already applied, implicit //wait when we launch the url, fluent wait, and explicit wait for a few elements and even we //have used thread.sleep function as well as a couple of places where the other 3 (selenium //waits) don't work.
We are having intermittent failures on the waits, although we have already applied, implicit //wait when we launch the url, fluent wait, and explicit wait for a few elements and even we //have used thread.sleep function as well as a couple of places where the other 3 (selenium //waits) don't work.
//When an element loads slowly, especially for asserting alert pop-up message validations?
`//Case 1:-> For implicit wait
//below code launch the application.**your text**
public class BrowserOperations
{
WebDriver driver;
public WebDriver LaunchApplication(String browserName) throws Exception
{
System.out.println("Browser \""+browserName+"\"");
switch(browserName)
{
case "Chrome":
DesiredCapabilities jsCapabilities = new DesiredCapabilities();
ChromeOptions chromeOptions = new ChromeOptions();
//chromeOptions.addArguments("--remote-allow-origins=*");
//chromeOptions.addArguments("--incognito");
Map<String, Object> prefs = new HashMap<>();
prefs.put("intl.accept_languages", "en-US");
chromeOptions.setExperimentalOption("prefs", prefs);
jsCapabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
Map<String, Object> prefsMap = new HashMap<String, Object>();
prefsMap.put("profile.default_content_settings.popups", 0);
prefsMap.put("download.default_directory", PropertiesfileDataProvider.GetPropertyValue("DownloadPath"));
chromeOptions.setExperimentalOption("prefs", prefsMap);
// need to uncomment below line for headless option
if(PropertiesfileDataProvider.GetPropertyValue("headless").equals("Yes")) {
chromeOptions.addArguments("--headless=new");
System.out.println("With headless browser");
}else
System.out.println("No headless browser");
driver=new ChromeDriver(chromeOptions);
break;
driver.manage().window().maximize();
System.out.println(driver.manage().window().getSize());
driver.manage().window().setSize(new Dimension(1440, 1024));
System.out.println(driver.manage().window().getSize());
System.out.println("URL: "+PropertiesfileDataProvider.GetPropertyValue("URL"));
driver.get(PropertiesfileDataProvider.GetPropertyValue("URL"));
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(60));
return driver;
//Case 2:-> explicit wait
//example login detail function through which user enters the details and enter to application
public void enterLoginDetails(String username, String password) throws InterruptedException {
WebDriverWait wait=new WebDriverWait(driver,Duration.ofSeconds(240));
wait.until(ExpectedConditions.elementToBeClickable(textbox_Username));
textbox_Username.sendKeys(username);
textbox_Password.sendKeys(password);
button_Login.click();
BaseClass.logger.info("Logged in to application");
System.out.println("Logged in to application");
selectInterfaceLanguage();
}
//case 2:-> fluent wait
//Below is the fluentwait which is written to handle different conditions
public void fluentwaitWithExpectedCondition(String expectedCondition, WebElement element) {
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver).withTimeout(Duration.ofSeconds(180))
.pollingEvery(Duration.ofSeconds(5)).ignoring(NoSuchElementException.class);
//Below are the conditions on which element will be actions
switch (expectedCondition) {
case "clickable":
wait.until(ExpectedConditions.elementToBeClickable(element));
break;
case "visible":
wait.until(ExpectedConditions.visibilityOf(element));
break;
case "stateToBe":
wait.until(ExpectedConditions.elementSelectionStateToBe(element, false));
break;
case "invisible":
wait.until(ExpectedConditions.invisibilityOf(element));
break;
default:
wait.until(ExpectedConditions.visibilityOf(element));
}
}
//below is my test script
@Test
public void testcaseaut()
{
try {
LoginWithUser();//Login function which uses browser operation class to launch the url for specific browsers.
if (driver.getCurrentUrl().contains(env1) || driver.getCurrentUrl().contains(env2))
{
NavigateTofeature(); // funtion to navigate to feature which is written in the same class
createAndDeleteUser("Ramana", "xuz", "[email protected]");// function to create user
featurePageObj.clickOnfeaturefMenu();
featureOBJ.clickOnUserdownloadInfoFromaddfeatureMenu();
Thread.sleep(5000);
Assert.assertTrue(featureOBJ.VerifyUserIsPresentOndownloadInFoPopUp("ramanaaddfeature@[email protected]"));
Thread.sleep(500);
featureOBJ.checkboxSelectAllPresentOndownloadUserInfoPopUp();
Thread.sleep(500);
featureOBJ.clickdownloadButtonOndownloadUsersInfoPopup();
featureOBJ.clickYesButtonOnConfirmAction();
logger.pass("User clicked on yes button for confirm action popup");
Thread.sleep(1000);
featureOBJ.clickviewJobLogsButtonOnJobStatusPopUp();
logger.pass("User clicked on view job logs button for job status popup");
Thread.sleep(7000);
applyFilterForUserOnLogScreenForJobTypeUserInfodownload();
Thread.sleep(5000);
logScreenForJobTypeUserInfodownloadAndClickOnDisplayLogForFirstRow();
infoMessageOnMessageTabForUserInfodownload();
infoMessageOnFilesTabForUserInfodownload();
jobLogObj = new addfeatureUserJobLog(driver);
String dateVar=jobLogObj.returnDateFieldInFormatFromFilesDisplayLogForUserdownloadLikeDisplayedOnlocaiInfoPopUp();
clickOndirectoryAndSelectWorkspaceThanNavigateTorequired();
featureOBJ = new requiredPage(driver);
featureOBJ.VerifyrequiredPage();
//driver.navigate().refresh();
Thread.sleep(5000);
featureOBJ.clickOnaddfeatureMenu();
Thread.sleep(3000);
featureOBJ.clickOnUserprocess();
Thread.sleep(5000);
//test case steps to verify that recovery date displayed for a user that is deleted and downloaded
featureOBJ.clickAndSendValueToSearchFieldOnlocaiUsersInfoPopup("ramanaaddfeature@[email protected]");
Thread.sleep(2000);
//jobLogObj = new addfeatureUserJobLog(driver);
jobLogObj.verifyenddateDateIsDisplayedOnlocaiUserInfoPopUp(dateVar);
jobLogObj.clickOnCancelButton();
Thread.sleep(2000);
createAndDeleteUser("Bamana", "addfeature", "bamanaaddfeature@[email protected]");
featureOBJ.clickOnaddfeatureMenu();
Thread.sleep(3000);
featureOBJ.clickOnUserprocess();
Thread.sleep(5000);
//test case steps to verify that recovery date should displayed for a user that is deleted and downloaded
featureOBJ.clickAndSendValueToSearchFieldOnlocaiUsersInfoPopup("bamanaaddfeature@[email protected]");
Thread.sleep(2000);
//verifyenddateDate
jobLogObj.verifyenddateDateNotDisplayedOnlocaiUserInfoPopUp(dateVar);
}
else
{
System.out.println("Not applicable for remaining urls");
}
} catch (AssertionError error) {
logger.fail(error.getMessage());
logger.fail(ExceptionUtils.getStackTrace(error));
Assert.fail(ExceptionUtils.getStackTrace(error));
} catch (Exception exception) {
System.out.println("Catch Block");
logger.fail(exception.getMessage());
logger.fail(ExceptionUtils.getStackTrace(exception));
Assert.fail(ExceptionUtils.getStackTrace(exception));
}
}`
The above code is trying to run script "testcaseaut
--
type here
", with some supportive functions, which are given in the above code. and code is explained. We are trying to ask that why the wait stretagy is not working.
Thanks for replying, I have tried removing implicit from the code, and running the script with only using explicit wait either for visible condition or for clickable condition with Duration of 120 sec
but my tests are intermittently failing (like out of 5, 2 time it fails at the same place where I put explicit wait) but if I apply Thread.sleep for few seconds, it works.
The above place it fails intermittently. What could be wait strategy for this. Any lead would be great help. Thanks in Advance.