For my current project, I am developing Selenium tests using Java to validate a straightforward PHP web application. A significant part of this testing involves populating various input fields.
However, when attempting to execute the test consecutively (twice or more), I encounter the following error:
org.openqa.selenium.ElementNotVisibleException:
Session ID: ef10680c-429b-4312-9d69-41496e7dce6a
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:127)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:93)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:42)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:163)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:601)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:274)
at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:84)
at com.midamcorp.insurance.TesterNGTest.test(TesterNGTest.java:83)
The issue lies in the fact that the element is hidden, despite me needing it visible for interaction. Initially obscured by Javascript, displaying it requires nothing more than clicking a link element, which is an action I replicate within my test script.
@BeforeClass
public static void setUpClass() throws Exception {
System.setProperty("webdriver.gecko.driver","C://geckodriver/geckodriver.exe");
driver = new FirefoxDriver();
driver.get(BASE);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void test() throws Exception {
// login
driver.findElement(By.id("ssn")).clear();
driver.findElement(By.id("ssn")).sendKeys("user");
driver.findElement(By.id("user")).clear();
driver.findElement(By.id("user")).sendKeys("pass");
driver.findElement(By.name("login")).click();
// insurance screen
driver.findElement(By.name("phone")).clear();
driver.findElement(By.id("phone")).sendKeys("phone");
driver.findElement(By.id("email")).clear();
driver.findElement(By.id("email")).sendKeys("email");
driver.findElement(By.id("empSSN")).sendKeys("ssn");
driver.findElement(By.id("address")).clear();
driver.findElement(By.id("address")).sendKeys("address");
driver.findElement(By.id("city")).clear();
driver.findElement(By.id("city")).sendKeys("city");
new Select(driver.findElement(By.id("state"))).selectByVisibleText("Missouri");
driver.findElement(By.id("zip")).sendKeys("63780");
driver.findElement(By.id("genderFemale")).click();
if(!(driver.findElement(By.cssSelector("input[type='checkbox")).isSelected())) { driver.findElement(By.cssSelector("input[type='checkbox")).click(); }
driver.findElement(By.cssSelector("input[value='1']")).click();
new Select(driver.findElement(By.name("employmentLocation"))).selectByVisibleText("Central Office");
driver.findElement(By.cssSelector("input.btn.btn-primary")).click();
// enrollment
// checks if the container for radio buttons is concealed; if so, triggers the display via a clickable link
if( !(driver.findElement(By.id("healthOnly")).isDisplayed()) ) {
driver.findElement(By.cssSelector("a[href='#healthOnly']")).click();
}
// the next line triggers the exception; the mentioned link was not clicked, leaving the radio button container invisible
List<WebElement> insuranceOptions = driver.findElements(By.cssSelector("#healthOnly input[type='radio']"));
insuranceOptions.get(rand.nextInt(insuranceOptions.size() - 1)).click();
insuranceOptions = driver.findElements(By.cssSelector("input[name='visionID']"));
insuranceOptions.get(rand.nextInt(insuranceOptions.size() - 1)).click();
To manage the visibility toggling in JavaScript/jQuery, I have created a simple function that performs as expected when manually activated:
$(".insuranceOptionsToggle").click(function(e){
e.preventDefault();
$($(this).attr("href")).toggle();
$(this).toggleClass("active");
});