Here is the test Scenario
- Step 1:- Enter username and password
- Step 2:- If the user is already logged in, A alert/popup will appear with a logout link and click the link to Logout and try to log in again.
This is the HTML CODE for that alert
<div class="top-alert hidden-xs hidden-sm">
<div id="alert0" class="alert pull-right alert-danger alert-dismissable" style="display:none;">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<strong>Error:</strong> Invalid Inputs!
</div>
<div id="alert2" class="alert pull-right alert-warning alert-dismissable" style="display:none;">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<strong>Warning:</strong> Your Login is limited
</div>
<div id="alert3" class="alert pull-right alert-warning alert-dismissable" style="display:none;">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<strong>Warning:</strong> You are Already Loggedin <a href="http://localhost/myapp/public/logout">Logout</a>
</div>
</div>
And here is the website screenshot
And here is my solution
public class Test
{
public static void main(String[] args)
{
System.setProperty("webdriver.chrome.driver", "C:\\\\Selenium\\\\drivers\\\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://localhost/myapp/public/login");
// Maxmimize windows
driver.manage().window().maximize();
// Login the Tripmaker
driver.findElement(By.xpath("//input[@id='email']")).sendKeys("test@test.com");
driver.findElement(By.id("password")).sendKeys("s123456");
driver.findElement(By.cssSelector("#lsbt")).click();
// Explicit wait
if (!driver.findElement(By.xpath("//div[@class='top-alert hidden-xs hidden-sm']/div[@id='alert3']/a[contains(text(), 'Logout')]")).isDisplayed())
{
WebDriverWait alert = new WebDriverWait(driver, 5);
alert.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='top-alert hidden-xs hidden-sm']/div[@id='alert3']/a[contains(text(), 'Logout')]")));
driver.findElement(By.xpath("//div[@class='top-alert hidden-xs hidden-sm']/div[@id='alert3']/a[contains(text(), 'Logout')]")).click();
}
else if (driver.findElement(By.xpath("//div[@class='top-alert hidden-xs hidden-sm']/div[@id='alert3']/a[contains(text(), 'Logout')]")).isDisplayed())
{
System.out.println("This step is skipped");
}
}
}
My code is working but if the alert showed while login its throwing an error
Exception in thread "main" org.openqa.selenium.TimeoutException: Expected condition failed: waiting for visibility of element located by By.xpath: //div[@class='top-alert hidden-xs hidden-sm']/div[@id='alert3']/a[contains(text(), 'Logout')] (tried for 5 second(s) with 500 milliseconds interval)
at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:113)
at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:283)
at demo.myapp.main(myapp.java:41)
Caused by: org.openqa.selenium.NoSuchElementException: Cannot locate an element using By.xpath: //div[@class='top-alert hidden-xs hidden-sm']/div[@id='alert3']/a[contains(text(), 'Logout')]
I Tried .isDisplayed()
, .isSelected()
and .isEnabled ()
.