0

There was another StackOverFlow question for which I had to write a program. My program was not working at all. The program is working fine when we debug it. It's failing at other times.

I assumed that it could be due to sync issues. So, changed all my known ExpectedConditions inbuilt methods as well as my own methods but none worked. Once I add a Thread.Sleep() for 1 sec, it worked.

Why is that sleep - customWait(1) required here? Can't we write this program without sleep?

public class Eljur {
    private static WebDriver driver;
    private static final Duration TIMEOUT_DURATION = Duration.ofSeconds(30);

    public static void main(String[] args) {
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://eljur.ru/login");
        customWait(1);
        findElement(By.cssSelector("div.form-select-button__placeholder")).click();
        findElement(By.cssSelector("input.form-input__input")).sendKeys("Y");
        findElements(By.cssSelector("div.form-select-option")).get(0).click();
        driver.quit();
    }

    public static void customWait(int seconds) {
        try {
            Thread.sleep(seconds * 1000L);
        } catch (Exception ignored) {
        }
    }

    public static WebElement findElement(By by) {
            return waitFor(elementToBeClickable(by));
    }

    public static List<WebElement> findElements(By by) {
            return waitFor(numberOfElementsToBeMoreThan(by,0));
    }

    public static<T> T waitFor(Function<WebDriver,T> function) {
            return new WebDriverWait(driver, TIMEOUT_DURATION)
                    .until(function);
    }
}

Selenide implementation also poses the same problem. Not working without the static wait.

open("https://eljur.ru/login");
Thread.sleep(1000);
$("div.form-select-button__placeholder").shouldBe(enabled).shouldBe(visible).shouldBe(interactable).click();
$("input.form-input__input").sendKeys("Y");
$$("div.form-select-option").get(0).click();
Selenide.closeWebDriver();

2 Answers 2

1

I observed that the app you are mentioning here needs to wait for its ssr tasks to finish which can be spotted from a small ssr attribute on one elemnt you can wait on it. The fixed code here as below.

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;
import java.util.List;
import java.util.function.Function;

import static org.openqa.selenium.support.ui.ExpectedConditions.elementToBeClickable;
import static org.openqa.selenium.support.ui.ExpectedConditions.numberOfElementsToBeMoreThan;

public class Main {
    private static WebDriver driver;
    private static final Duration TIMEOUT_DURATION = Duration.ofSeconds(30);

    public static void main(String[] args) {
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://eljur.ru/login");
        waitFor((iDriver -> findElement(By.cssSelector("astro-island")).getAttribute("ssr")==null));
        findElement(By.cssSelector("div.form-select-button__placeholder")).click();
        findElement(By.cssSelector("input.form-input__input")).sendKeys("Y");
        findElements(By.cssSelector("div.form-select-option")).get(0).click();
        driver.quit();
    }

    public static void customWait(int seconds) {
        try {
            Thread.sleep(seconds * 1000L);
        } catch (Exception ignored) {
        }
    }

    public static WebElement findElement(By by) {
        return waitFor(elementToBeClickable(by));
    }

    public static List<WebElement> findElements(By by) {
        return waitFor(numberOfElementsToBeMoreThan(by, 0));
    }

    public static <T> T waitFor(Function<WebDriver, T> function) {
        return new WebDriverWait(driver, TIMEOUT_DURATION)
                .until(function);
    }
}```
Sign up to request clarification or add additional context in comments.

Comments

1

You have to just find the parent element for the div.form-select-button__placeholder

Below is the selenide code that worked!

@Test
void demoTest() {
  Selenide.open("https://eljur.ru/login");
  $("div.form-select-button__placeholder")
      .parent()
      .shouldBe(Condition.visible)
      .click();
  $("input.form-input__input").shouldBe(Condition.visible)
      .sendKeys("Y");
  $$("div.form-select-option")
      .shouldHave(CollectionCondition.sizeGreaterThan(0))
      .get(0).click();
}

1 Comment

Thanks Amuthan. I tried in 2 different machines Mac M1, Mac Intel i7. Facing issues on both. So, am not making it as accepted for now.

Your Answer

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.