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();
Ajeet Verma
  • 2,938
  • 3
  • 13
  • 24
Ahamed Abdul Rahman
  • 512
  • 1
  • 5
  • 18

1 Answers1

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();
}
Ajeet Verma
  • 2,938
  • 3
  • 13
  • 24
Amuthan
  • 11
  • 1