Selenium tests run too fast without waiting for a select switch

I have a Selenium Grid test and WebDriver 2.48.2 that runs too fast. Most of the time the test stops because the switch is not selected until the button is pressed.

Radio notes are configured using JavaScript based on the JSON file to create any number of them in the on-the-fly section. After clicking the Continue button, this section will be destroyed, and a new one will be created using the new switches.

I tried implicit waiting without success.

driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

The only solution that works for me is a delay that allows enough time for the switch to click.

driver.findElement(By.id("radiobuttonid")).click();
Thread.sleep(delay);

But, in my opinion, this is not an ideal solution, perhaps the delay may be insufficient or too long, wasting time; there can be any number of switches so that the time increases exponentially.

I tried setting various explicit expectations with different expected conditions , but without success.

I tried to wait for the creation of the radio button, as it may not exist ( presenceOfElementLocatedand elementToBeClickable). I tried to wait for his choice ( elementToBeSelected).

I had trouble finding what the expected conditions should do, as the descriptions are short and open to misinterpretation.

Ideally, I want the test to continue shortly after pressing the switch. If possible, what is the best way to do this?

EDIT

L.Bar , , , , .

2

. , .

private static Predicate<WebDriver> forceSelectionOfElement (final String id)
{
    return new Predicate<WebDriver>()
    {
        @Override
        public boolean apply(WebDriver arg0)
        {
            WebElement element = arg0.findElement(By.id(id));                
            boolean rval = element.isSelected();

            if (!rval) {
                element.click();
            }

            return rval;
        }
    };
}

WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(forceSelectionOfElement("q___01_02_01___1___a"));

, , , :)

import com.google.common.base.Predicate;
+4
3

, 'click' - . , , , . WebDriverWait, , .

WebDriverWait wait = new WebDriverWait(driver, 5);
        final By lookup = By.id("radio");
        LOG.debug("Wait for radio to be clickable.");
        wait.until(ExpectedConditions.elementToBeClickable(lookup)); //I assume this implies visibility.
        LOG.debug("Element clickable.  Proceeding into click behavior.");
        wait.until(new Predicate<WebDriver>() {
            @Override
            public boolean apply(WebDriver arg0) {
                LOG.debug("Resolving 'myId' Element");
                WebElement radio = arg0.findElement(lookup);                
                boolean rval = radio.isSelected();
                LOG.debug("Element Resolved and has a state of " + (rval ? "selected" : "not selected"));
                if (!rval) {
                    LOG.debug("Clicking on the Element!");
                    radio.click();
                }
                //If we return false we will loop.  So the first time through we let this click the radio for us.
                //Second time through we should find that the element is clicked.  If it not we click it again until it represents the state we're wanting.
                return rval;;
            }});

  • WebElement
  • WebElement,
  • .

, , , , . , .

, Thread.sleep . , ExplicitWait. , , .

Thread.sleep , wait, webdriverwait ?

!

+2

:

public bool IsElementPresent(By by, IWebDriver driver)
{
    try
    {
        driver.FindElement(by);
        return true;
    }
    catch (NoSuchElementException)
    {
        return false;
    }
}

public bool IsElementPresent(By by, IWebDriver driver, int sec)
{
    bool itemExist = false;

    itemExist = IsElementPresent(by, driver);
    while (!itemExist && sec >= 0)
    {
        Thread.Sleep(1000);
        itemExist = IsElementPresent(by, driver);
        sec--;
    }

    if (sec == -1)
        return false;
    else
        return true;
}

,

//Checking if element exist 10 sec
if(IsElementPresented(By.Id("radiobuttonid"),driver,10))
{
//element exist click on it
}
else
{
//didn't exist.
}
+3

, ExpectedConditions, ExpectedConditions.elementToBeSelected()? , . -

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement radio = driver.findElement(By.id("radiobuttonid"));
radio.click();
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id("ID of message element")));
// do stuff

OP

+1

Source: https://habr.com/ru/post/1611615/


All Articles