I have a problem that I can’t get through. I just started to use Selenium, and I made a simple JUnit test with it. (Open CMS admin panel, log in and log out.) If I use the correct username and password it works like a charm, but if I don’t, it just can’t get over finding the “Log out” button, however I’ve put it into try-catch block. :/ Actually it just stops by the if statement (see the code below) without throwing the exception, and it won’t close the browser and finish the test because it never reach “driver.quit()”.
If you have any idea about my problem, please help me out!
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
public class JavaExp {
private WebDriver driver;
private String baseUrl;
private StringBuffer verificationErrors = new StringBuffer();
@Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "http://urlhere.com/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void testJValami() throws Exception {
driver.get(baseUrl + "/administrator/");
driver.findElement(By.id("mod-login-username")).clear();
driver.findElement(By.id("mod-login-username")).sendKeys("admin");
driver.findElement(By.id("mod-login-password")).clear();
driver.findElement(By.id("mod-login-password")).sendKeys("almakfa");
driver.findElement(By.linkText("Belépés")).click(); //Click on the login Button
if(isElementPresent(By.linkText("Kilépés"))) { //Looking for the Log out link
System.out.println("Got it.");
} else {
System.out.println("Not found.");
}
}
@After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
private boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}
}
well, the matter is that after you press ‘login’ button page is supposed to render.
So in your case I would try different wait mechanisms:
driver.findElement(By.linkText("Belépés")).click(); //Click on the login Buttondriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
if(isElementPresent(By.linkText("Kilépés")))
{ //Looking for the Log out link
System.out.println("Got it.");
} else {
System.out.println("Not found.");
}
driver.findElement(By.linkText("Belépés")).click(); //Click on the login ButtonThread.sleep(1000);
if(isElementPresent(By.linkText("Kilépés")))
{ //Looking for the Log out link
System.out.println("Got it.");
} else {
System.out.println("Not found.");
}
public WebElement fluentWait(final By locator){Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
.withTimeout(30, TimeUnit.SECONDS)
.pollingEvery(5, TimeUnit.SECONDS)
.ignoring(org.openqa.selenium.NoSuchElementException.class);
WebElement foo = wait.until(
new Function<WebDriver, WebElement>() {
public WebElement apply(WebDriver driver) {
return driver.findElement(locator);
}
}
);
return foo; } ;
driver.findElement(By.linkText("Belépés")).click(); //Click on the login Button
fluentWait(By.linkText("Kilépés"));
if(isElementPresent(By.linkText("Kilépés")))
{ //Looking for the Log out link
System.out.println("Got it.");
} else {
System.out.println("Not found.");
}