Quantcast
Channel: Active questions tagged selenium - Stack Overflow
Viewing all articles
Browse latest Browse all 97793

Why aren't alerts modal and closed in my Electron application when tested with Selenium?

$
0
0

I want to test a web application with the selenium framework using java. When I test the application using Selenium with Chrome and Selenium with an Electron wrapper (as described here: Testing Electron application with org.openqa.selenium in a Java environment (Intellij)), the behavior between the two solutions is different.

In specific, modal alerts are correctly closed using Selenium with Chrome but they somehow remain open using Selenium with Electron. When I use Selenium with Electron, they don't even seem to be modal at all - I can simply interact manually or programmatically with the rest of the web application. When I open the Electron application manually the alerts are modal.

I use the following (very experimental) minimum working java code snippet to test the application (I can't share much detail regarding the application itself though as it doesn't belong to me):

import java.util.List;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Test
{
    private final WebDriver driver;

    private final WebDriverWait wait;

    public static void main(final String[] args)
    {
        final Test test = new Test();

        test.execute();
    }

    public Test()
    {
        super();

        this.driver = this.createChromeDriver();
        this.wait = new WebDriverWait(this.driver, 5);
    }

    private WebDriver createChromeDriver()
    {
        System.setProperty("webdriver.chrome.driver", "[PATH_TO_CHROME_DRIVER]");

        final ChromeOptions options = new ChromeOptions();
        // I use this only when I work with Electron.
        options.setBinary("[PATH_TO_ELECTRON_APPLICATION]");

        final WebDriver driver = new ChromeDriver(options);
        return driver;
    }

    private void execute()
    {
        // I use this only when I work with Chrome.
        // this.driver.get("[URL_TO_WEB_APP]");

        this.provokeAlerts();

        this.wait(3000);

        this.driver.quit();
    }

    private void provokeAlerts()
    {
        final By deleteLocator = By.xpath("[PATH_TO_DELETE_BUTTON]");
        this.wait.until(ExpectedConditions.elementToBeClickable(deleteLocator));
        final List<WebElement> deleteButtons = this.driver.findElements(deleteLocator);
        this.clickOnFirstElement(this.driver, deleteButtons);
        this.wait.until(ExpectedConditions.alertIsPresent());
        final Alert alertForDismiss = this.driver.switchTo().alert();
        alertForDismiss.dismiss(); // Electron doesn't close the alert, Chrome does. We can simply go on working, even though the dialog is modal.

        this.wait.until(ExpectedConditions.elementToBeClickable(deleteLocator));
        this.clickOnFirstElement(this.driver, deleteButtons);
        this.wait.until(ExpectedConditions.alertIsPresent());
        final Alert alertForAccept = this.driver.switchTo().alert();
        alertForAccept.accept(); // Electron doesn't close the alert, Chrome does. We can simply go on working, even though the dialog is modal.

        this.provokeAlerts();
    }

    private void clickOnFirstElement(final WebDriver driver, final List<WebElement> elements)
    {
        if (!elements.isEmpty())
        {
            this.clickOnElement(driver, elements.get(0));
        }
    }

    private void clickOnElement(final WebDriver driver, final WebElement element)
    {
        if (this.isInteracteable(element))
        {
            final Actions actions = new Actions(driver);
            actions.moveToElement(element).click().build().perform();
        }
    }

    private boolean isInteracteable(final WebElement element)
    {
        return element.isDisplayed() && element.isEnabled() && element.getSize().height > 0 && element.getSize().width > 0;
    }

    private void wait(final int milliseconds)
    {
        try
        {
            Thread.sleep(milliseconds);
        }
        catch (final InterruptedException e)
        {
            // ignore
        }
    }
}

Why would the alerts not be modal in Selenium with Electron? Why would they remain visible in that case even though I dismiss or accept them? Is that an error in my code or setup?


Viewing all articles
Browse latest Browse all 97793

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>