#Test
public void loginWithEmptyFields() throws InterruptedException {
driver.findElement(By.linkText("Tu Log In / Register")).click();
driver.findElement(By.name("j_username")).clear();
driver.findElement(By.name("j_username")).sendKeys("");
driver.findElement(By.name("j_password")).clear();
driver.findElement(By.name("j_password")).sendKeys("");
Thread.sleep(1000);
driver.findElement(By.className("loginButtonMain")).click();
Thread.sleep(1000);
}
Assert code should check whether both username and password fields are empty, if the fields are empty the login button is disabled and should get the title the page which is 'Login - Tu Clothing'.
However the below code is not working please help!
boolean actualEmptyLoginButton =
driver.findElement(By.cssSelector(".loginButtonMain")).isEnabled();
Assert.assertTrue(actualEmptyLoginButton, "Login Button is not displayed");
String actualEmptyLoginTitle = driver.getTitle();
Assert.assertEquals("Login | Tu clothing", actualEmptyLoginTitle);
As far as I understand your question, you are using Selenium to verify that the title of your login button has changed, after the fields for username and password are cleared.
It looks like some JavaScript is executed, when the content of the text field is cleared.
So I recommend to use a WebDriver for a real browser (e.g. Firefox). Also avoid Thread.sleep(). Better use an explicit wait for the condition, see more at the Selenium documentation for explicit and implicit waits.
Related
I'm trying to write a simple java based selenium code where I would load a page, give the desired values to username & password.
Once the page loads username is already focused but I can enter values into username or password
Using MAC - Eclipse
I am new to coding sorry so any help would be greatfully received
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
WebDriver driver = new FirefoxDriver();
driver.get("http://dc1.racbusinessclub.co.uk/login/");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
Thread.sleep(2000);
driver.findElement(By.tagName("body")).sendKeys("rac-dc");
Thread.sleep(2000);
driver.findElement(By.tagName("body")).sendKeys(Keys.TAB);
Thread.sleep(2000);
Below is basic way of doing it.
driver.findElement(By.id("username")).sendKeys(username);
driver.findElement(By.id("password")).sendKeys(password);
pause(3);
WebElement button = driver.findElement(By.tagName("button"));
button.sendKeys(Keys.RETURN);
And once logged-in you can assert the url like below
Assert.assertTrue(driver.getCurrentUrl().endsWith("/yourURL"));
You can get username and password id from the browser by Right click on the page and select Inspect.
Even if the field is already focused, you must specify which element you wish to "send the keys" to. So grab the id of the username field, and then use sendKeys, like so:
driver.findElement(By.id("userNameInputBox")).sendKeys("rac-dc");
Also, worth noting that using Thread.sleep in your scripts is bad practice. If there's something you're waiting for, check for the presence of that element, rather than waiting an arbitrary amount of time.
Edit: Actually, seeing your response above I can see you're trying to interact with an alert popup, which you can do with:
driver.switchTo().alert().sendKeys(yourString);
Alternatively, you can skip that entirely and navigate to the page with your credentials in the URL - remember not to hard-code these:
driver.get("http://username:password#http://dc1.racbusinessclub.co.uk/");
Goal: alert pop up. whether it's shown or not, I want it to continue. if it shows, have to select the checkbox, and hit continue. if not, ignore.
Blocker: if alert shows, it will handle the action and dialog will be closed. but when it's not, selenium hangs there without handling condition when it's not shown.
background: I use UFT before, maybe my logic could be wrong.
the pop up is application alert(not systems), so assumed "switch to(),accept()/dismiss() won't work. and I will add handle alert right after login and within the login method below.
Selenium framework background. : we use selenium maven framework, serenity BDD. object is set at the beginning of the page. and serenity.properties handle the time out default and so on.
Pop up objects (if it appears):
#FindBy(xpath = "//input[#id='notification-ack']")
private WebElement PcoNoticeChbx; //this is a check box, needs to be checked
#FindBy(xpath = "//button[contains(.,'Continue')]")
private WebElement PcoNoticeContinueBtn;//button to close the alert
*Log in method *
public void loginIntoExtApplication(String baseURL, String loginURL, String uId, String pwd, String extAppEnv)throws Exception {
gotoExtLoginPage(baseURL, loginURL);
enterLoginCredential(uId, pwd);
openAt("https://" + extAppEnv + ".programportaltest.hrsa.gov/sdms-
extranet/index.xhtml");
My Approaches:
//1.
if (PcoNoticeChbx!=null) {
PcoNoticeChbx.click();
PcoNoticeContinueBtn.click();
} else {
System.out.println("Dialog didn't display, no need any action");
}
//2. hanged here after login actions.
if(!getDriver().findElements(By.xpath("//*[#id='submit']")).isEmpty()){
PcoNoticeChbx.click();
PcoNoticeContinueBtn.click();
}
else {
System.out.println("Dialog didn't display, no need any action");
}
//3. added to watch doesn't work, it shows pending, below code failed too. I ran my Maven in Junit in debug mode. it used to work fine. but watch elements always show (pending)..
boolean isPresent = getDriver().findElements(By.id("noticebox")).size() >0
System.out.println("the diaolog exist= " + isPresent);
//4. even tried the try-catch method.
try{
PcoNoticeChbx.click();
PcoNoticeContinueBtn.click();
}catch (Exception e){
// Printing logs for my report
Log.error("Report Category button element is not found.");
// After doing my work, now i want to stop my test case
throw(e);
}
return;
}
//5. tried list webelemets:
List temp = webdriver.findElements(org.openqa.selenium.By.id("noticebox"));
if (temp.Count > 0 && temp[0].Displayed) {
// script to execute if element is found
} else {
// continue the test
}
//6. and below
if (!WebDriver.findElements(By.xpath("//*[#id='submit']")).isEmpty()==true);
{
//handle the dialog
}
else{
//continue
}
// 7.tried with a boolean value, but it also hangs on here first steps
boolean Nbox = PcoNoticeChbx.isDisplayed(); {
if (Nbox==false)
{
System.out.println("Dialog didn't display, no need any action");
}
else if (Nbox==true) {
PcoNoticeChbx.click() ;
PcoNoticeContinueBtn.click();
}
If this is like the popups that I've dealt with they work like this:
Customer comes to site and the site checks for the existence of a cookie. If that cookie exists, the popup is never launched (ideal state). If that cookie does NOT exist (typical state), after a specified period of time a popup appears.
Customer dismisses the popup and a cookie is created with an expiration time
Once that expiration time passes, the cookie expires and the popup will fire again
You need to do some investigation and find the cookie that is created once the popup is dismissed. Clear your cache, browse to the site, and note all the cookies that exist. Wait for the popup and dismiss it. Now find the cookie that was just created and examine it. This is the cookie you need to create. There are a lot of tutorials on creating cookies. It's pretty straightforward.
Once you learn how to create the cookie, you add it to your script as described below:
Navigate to some page on the domain that you know doesn't exist, e.g. www.domain.com/some404page. We do this because it won't trigger the popup countdown and we need to be on the domain to create the cookie.
Create the cookie
Do your normal test
No more popups.
Solution found for my case.
this maybe very easy. but takes some times for me to research. hopefully it will help you.
after use of many methods, for this javascripts confirmation alert. i have used below method. all of help of .CurrentlyVisible() method. because this one, and i guess only this one will give you result even when the element does not exist or null..
if (element(NoticeContinueBtn).**isCurrentlyVisible**()==true) {
PcoNoticeChbx.click();
PcoNoticeContinueBtn.click();
//else just continue
}
Is it possible in Selenium Webdriver to pause the code execution using webdriver.wait until the user clicks the login button for a form?
The form contains a Captcha that the user has to manually fill in, so I can't automatically click the button using the script.
The login button on click returns the value of a JavaScript function in the form of Boolean value i.e either true or false.
Any possible solution for this problem?
Edit: completely new response after getting more info.
With the code below, this is what happens:
Selenium enters the user data
I then manually refresh the page (because I don't have login data to test)
Selenium enters the user data again
(the variable W is an earlier defined WebDriverWait.)
driver.Navigate().GoToUrl("https://www.irctc.co.in/eticketing/loginHome.jsf");
// will try looping as long as you're on the relevant page
do
{
try
{
IWebElement username = w.Until(ExpectedConditions.ElementIsVisible(By.Id("usernameId")));
IWebElement password = driver.FindElement(By.Name("j_password"));
if (String.IsNullOrEmpty(username.GetAttribute("value")))
{
username.SendKeys("a");
password.SendKeys("b");
}
}
catch (Exception)
{
// page is reloading, just wait another round
}
} while (!String.Equals(driver.Url, "put url after login here"));
I am trying to automate a web page using selenium WebDriver. I can move towards the webpages and can able to do all tasks on parent pages. But at one place I am getting a POPUP window which asks username and password. I am unable to identify the username and password text box id's, how can I find the element (TextBox) and send the username and password through selenium webDriver code.
You can try this by using your web URL,
#Test
public void testlinkedin() throws Exception {
driver.get("https://www.linkedin.com/"); //Use your web URL
driver.findElement(By.linkText("Sign Up")).click();
driver.findElement(By.cssSelector("button.fb-btn")).click();
Thread.sleep(3000);
Set <String>handles = driver.getWindowHandles();//To handle multiple windows
firstWinHandle = driver.getWindowHandle();
handles.remove(firstWinHandle);
String winHandle=handles.iterator().next();
if (winHandle!=firstWinHandle){
secondWinHandle=winHandle;
driver.switchTo().window(secondWinHandle); //Switch to popup window
Thread.sleep(2000);
driver.findElement(By.id("email")).clear();
driver.findElement(By.id("email")).sendKeys("Username");
driver.findElement(By.id("pass")).clear();
driver.findElement(By.id("pass")).sendKeys("Password");
driver.findElement(By.id("u_0_2")).click();
driver.findElement(By.name("__CONFIRM__")).click();}
Make sure to declare these string variables as,
public String firstWinHandle;
public String secondWinHandle;
You need to switch to the new pop-up window to select its elements. Here is an example to switch to the new window:
String handle = driver.getWindowHandles().toArray()[1]; // 1 stands for the new window and 0 for the parent window
driver.switchTo(handle);
Now you can select your elements present in this window.
*Note: * To get back to original window you again need to switch back to it with 0 as the index.
Maybe the popup window is HTTP basic authenticated site. If so, then you cannot use the window handles as stated in previous answer, but in that case you have to send the username and password directly to URL request:
driver.get("http://username:password#your-test-site.com");
Its working fine for me as I used like this...
String handle = driver.getWindowHandles().toArray()[1].toString();
driver.switchTo().window(handle);
I am testing a web application and my sample test case has three steps:
Enter Username
Enter Password
Click 'Login'
driver.findElement(By.id("username")).sendKeys(username);
driver.findElement(By.id("password")).sendKeys(password);
driver.findElement(By.id("LoginButton")).click();
I need to generate a report which specifies which steps passed and which failed. Is there some mechanism by which I can know that each of those lines executed successfully? Does Selenium have some kind of inbuilt Activity/Event Listener?
If you want log all basic steps like click, navigate, on find element, etc
I recommend you add some event listener. You can use the class EventFiringWebDriver.
An example:
EventFiringWebDriver events = new EventFiringWebDriver(this._driver);
events.Navigated += new EventHandler<WebDriverNavigationEventArgs>(eventAfterNavigate);
and you can define the event handler as
private void eventAfterNavigate(object sender, WebDriverNavigationEventArgs e)
{
Log("URL visited: " + e.Driver.Url); // Call to your favourite log method
}
You can find more possible event handlers in the Webdriver API reference: http://selenium.googlecode.com/svn/trunk/docs/api/dotnet/html/AllMembers_T_OpenQA_Selenium_Support_Events_EventFiringWebDriver.htm
You may use either Verify or Assert to be sure that something is present or absent on the page. Verify will return you Boolean and continue running while Assert on false will stop you test(s).
In this particular case you may verify that you typed username into element with id=username. So your code could look like this:
driver.findElement(By.id("username")).sendKeys(username); // this will enter username
assertEquals(selenium.getValue("id=username"), username); // this will check that text in your username field is 'username' (also you may use Assert.IsTrue)
As far as I know there is verifyTextPresent in Selenium 2 Webdriver, so you may use this:
try
{
assertEquals(selenium.getValue("id=username"), username); // put message into your log: success
}
catch (Exception)
{
// put message into your log: failed
}
I suggest to verify Button click but waiting for some notification that you logged in: e.g. text "Hello, {username}" might appear, or new element (absent before log in) might appear as well. So if this element is present - you are definitely logged in.
If you want to see if each step is successful, then I put some asserts (depending on your unit testing framework) to verify that the steps succeeded.
For example, to check that the username field actually received input, you could immediately verify that the username field has the text of whatever the username is that was typed in.
Some pseudo code:
IWebElement element = driver.findElement(By.id("username"));
element.sendKeys(username);
assertsEquals(username, element.text);
Likewise, to verify that a click was successful, you could check and assert that an element that is expected on the next page exists, which would then indicate that the login was successful.
If Selenium dose not throw Exception then lines are executed successfully.
But question is how do you define success? I would check site that was loaded after click on LoginButton to be sure that behavior of web application is like you expect.