Selenium Testing - Text addition via pop up window is not getting saved - java

I am trying to automate a website using selenium web driver.
On the website, there is a popup form for creating a gate name with a text field to enter the text. Using selenium I tried to do, the pop up came but the given text was not getting saved. To pop up it's working correctly. Need guides to complete the action.
Methods I used are given below:
Alert alert=driver.switchTo().alert();
driver.switchTo().alert().sendKeys("New Gate");
alert.accept();
System.out.println(alert.getText());
Elements are identified by the Xpath element locator.
I also gave text along with URL, another method that I got from the stack.

If its an alert then right way handle as per your requirement steps should be
switch to and alert
enter text
get entered text
accept
Alert alert=driver.switchTo().alert();
alert.sendKeys("New Gate");
System.out.println(alert.getText());
alert.accept();`
If its a model popup then try to locate popup element and directly used send keys
sample code :
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.your_popup_locator));
driver.findElement(By.you_textbox_locator).sendKeys("expected text");

You need to switch to pop-up first. Then you can access elements in the pop-up.
// save your main window handle
String MainWindow=driver.getWindowHandle();
// Get all window handle
Set<String> handles = driver.getWindowHandles();
Now, for each childHandle in handles,
driver.switchTo().window(childHandle );
// check if your textField present.
// If found, do your actions and break;
// else switch to next window
At last, switch to main window again.
driver.switchTo().window(MainWindow);

Related

In Selenium java how to handle the pop up

I use selenium 2 and the browser version is IE 11. I face issue while handling with pop up.
Scenario 1:
driver.findElement(By.id("I create modal window")).click();
Set<String> windows = driver.getWindowHandles();
System.out.println(windows.size());
I get the output as 2.
Scenario: 2 (with the same concept)
driver.findElement(By.id("I create pop up")).click();
Set<String> windows = driver.getWindowHandles();
System.out.println(windows.size());
I get the output as 1.
I could not switch to the pop-up. Some times the pop-up response like not connected and moreover the size of the popup is way smaller than what it should be.
What would be the problem
Note : The element id's are not real one
You have to first check its window or alert.
Simply try to inspect element in that popup, if you are able to inspect elements in popup then its window so use switch to window command, if not then it is alert. So use switch to alert command (which provided in another answer)
From your question it looks like you wanted to switch to a pop - up.
Window handler is used to switch between browser windows.
Alert handler is used to switch between the pop - up messages.
If you wanted to accept/yes/ok the alert:
Alert alert = driver.switchTo().alert();
alert.accept();
If you wanted to dismiss/no/cancel the alert:
Alert alert = driver.switchTo().alert();
alert.dismiss();
There are different types of alerts and it should be handled differently as given below.
Windows type:
if your popup opened in a new window, you can handle it using get window handle and switch to window methods.
driver.selectElement(By.id("I create modal window")).click();
Set<String> windows = driver.getWindowHandles();
System.out.println(windows.size());
for( String window : windows)
driver.switchToWindow(window);
Javascript alert:
if the popup is javascript type. you can use switch to alert method.
Alert alert = driver.switchTo().alert();
DOM Element type:
sometimes the popup is like DOM Element, created dynamically or hidden until popup. It can be handled using find elemeent method.
driver.findElement(locator).Click();
I haven't fix my problem, but I found interesting things : when i open a popup, it is not connected to the current session if i open my web site with Selenium, even if don't ask Selenium to open my popup but i do it my self. If id do :
driver.quit()
the Main window will be close but not the popup

Selenium Java Chrome send enter key while the elements on the page are disabled

I need to close a dialog box that pops up after I had selected 'Save' by clicking 'Ok', however none of the elements on the page as well as dialog box can be inspected by right clicking after the pop-up (tried F12 doesn't help).
Alternatively, to close the dialog box enter key could be given, however I'm unable to send the enter key as listed below.
Actions action = new Actions(driver); //attempt 1
action.sendKeys(Keys.RETURN);
action.sendKeys(Keys.RETURN).perform(); //attempt 2
Both actions does not close the dialog box. I had performed the driver switch as well. In addition to the dialog box pop-up there's also an outlook email pop-up could this create an issue in identifying the alert pop-up? Kindly advise on how enter key could be passed to close the dialog box.
Found the issue, it was due to Thread.sleep(3000) that I had used after the click, guess this caused it to miss the alert from being recognized. I might be worng as well. Thanks for all your help!
As none of the elements can be inspected by right clicking after the pop-up which indicates its an Alert which is Javascript generated and you can use the following line of code :
Alert myAlert = new WebDriverWait(driver, 10).until(ExpectedConditions.alertIsPresent());
myAlert.accept();
Update A
As per the comment update :
Observation : In addition to the alert msg there's also outlook mail also pops up
Conclusion : If outlook mail pops up possibly it's not an Alert as suspected. You should be able to track the element within the HTML DOM.
Update B
As per the comment update :
Observation : there are 2 thing happens when i click on 'Save': [1]: Outlook email pop-up [2]: Alert notification on the saved page stating the records are being updated
Conclusion : Sounds like two window_handles opening up, treat them as window_handles.
Solution 1 : Try switching to the pop up and handle it.
new WebDriverWait(driver, 15).until(ExpectedConditions.alertIsPresent());
driver.switchTo().alert().accept();
Solution 2 : If you want to go with Keyboard keys.
Actions action = new Actions(driver);
new WebDriverWait(driver, 15).until(ExpectedConditions.alertIsPresent());
action.sendKeys(Keys.RETURN).perform();
In both cases, use 'wait' until you get an alert.
First, check the dialog box that pops up after you click 'Ok' is JavaScript Alert or anything else?
If the dialog is JavaScript Alert then try below code.
new WebDriverWait(driver, 10).until(ExpectedConditions.alertIsPresent());
Alert alert = driver.switchTo().alert();
alert.accept();

How to insert text in smart textbox in selenium webdriver?

I am trying to automate the code of submit resume page, it consists on smart textboxes, which gives suggestions below as soon as you type few text in it. you need to select and input into the textbox from the suggestions given. Below is the code and the url:
WebDriver w= new FirefoxDriver();
w.get("https://www.hrmantra.com/LetsLead/18_Recruitment/SubmittResume.aspx?cn=LetsLead");
w.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
w.findElement(By.id("StCityName_txtSpeedName")).sendKeys("Mumbai");
Only sendkeys command is not working as the entered value has to be selected and the control needs to be closed.
The text box has a select drop down box which appears dynamically on entering the city from it the user selects his city but this select box is inside an iframe (iframe id : SpeedTyperFrameID)so we need to switch to it and then access the select box
Below is the code
WebElement city = driver.findElement(By.xpath("//*[#id='StCityName_txtSpeedName']"));
city.click();
city.sendKeys("chennai");
//wait for the iframe to load and then switch to it
new WebDriverWait(driver, 20).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id("SpeedTyperFrameID")));
Thread,sleep(3000);//added just to show u the effect remove it
WebElement byValue = driver.findElement(By.id("SelectList"));
//using select class to select the element by its text
Select select = new Select(byValue);
select.selectByVisibleText("Chennai");
//switch back to default content inorder to access other elements outside the iframe
driver.switchTo().defaultContent();
I have tested the above code it is working fine
Kindly get back if you have any queries.
Ideally, After you type partial text into input you have to find all suggestions from dropdown list and click on it. Perhaps, try to use enter key, but I am not aware that it will help
element.sendKeys("Mumbai" + Keys.ENTER)
It's because there is no ID of id=StCityName_txtSpeedName on the page. You don't use id=... for the ID, you just type the ID.
See below.
w.findElement(By.id("StCityName_txtSpeedName")).sendKeys("Mumbai");

How to handle a pop up which is a simple HTML pop up in selenium webdriver?

How to handle a pop up which is a simple HTML pop up in selenium webdriver . Its a pop up written in HTML .
In case the pop up is modal you have to do what the previous answer is proposing, but in case the pop up is just HTML you should just find a WebElement inside the popup code and work as usual, look the example:
driver.findElement(By.id(popupid))
Pay attention and manage the implicit timeout in order to make sure the find will wait until the popup starts.
Have you tried using
driver.switchTo().alert().accept();
?
Use below given sample code and it will work.
public String parentWindowHandler = null,subWindowHandler=null;
Set<String> handles = driver.getWindowHandles(); // get all window handles
Iterator<String> iterator = handles.iterator();
while(iterator.hasNext()){
subWindowHandler = iterator.next();
driver.switchTo().window(subWindowHandler);//select new popup
}
/*
your code here for script
*/
driver.switchTo().window(parentWindowHandler);//return to main window
First, you should find unique DOM properties because of sometimes same element properties like id and class same for HTML pop-up and parent Page.
So If it's unique then u can perform simply drive.findelements(By).click() , or sendkeys(), etc else u should be made unique XPath or by indexing, u have to click or perform other action.
SwitchTo uses if it is in iframe else it will not use.

Not able to switch to pop-up window and find any elements in pop-up in Webdriver using Java

I have an application in which i tried clicking on a button and in return it will pop-up a window for filling a new user form. This is not really like a pop-up window, because it has some input fields as well as "save " and "cancel" button. It looks similar to pop-up window in facebook.
Here the code i tried with
Set beforePopup = driver.getWindowHandles();
driver.findElement(By.xpath("/html/body/div/div/div/div[3]/div/div[2]/div[2]/table/tbody/tr/td[3]/table/tbody/tr/td[2]/em/button")).click();
driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
Set afterPopup = driver.getWindowHandles();
afterPopup.removeAll(beforePopup);
if(afterPopup.size() == 1) {
driver.switchTo().window((String)afterPopup.toArray()[0]);
}
//driver.switchTo().window("Add New User");
//selenium.type("userDetailsBean.firstName","alan1");
//WebElement btnStartQueryInside = driver.findElement(By.xpath("//html/body/div[14]/div/div/div/div/span"));
//btnStartQueryInside.getText();
System.out.println(driver.getTitle());
WebElement firstName = driver.findElement(By.xpath("/html/body/div[2]/div/div/div/div/div/form/div/div/input"));
firstName.sendKeys("alan1");
//driver.switchTo().alert();
//driver.findElement(By.xpath("//html/body/div[14]/div/div/div/div")).getText();
//WebElement firstName=driver.findElement(By.xpath("/html/body/div[2]/div/div/div/div/div/form/div/div/input"));
//firstName.sendKeys("alan1");
/*WebElement lastName=driver.findElement(By.id("userDetailsBean.lastName"));
lastName.sendKeys("harper");
WebElement emailadd=driver.findElement(By.id("userDetailsBean.userEmail"));
emailadd.sendKeys("alan1#derik.com");
WebElement username=driver.findElement(By.id("userDetailsBean.userName"));
username.sendKeys("xalan1");
WebElement password=driver.findElement(By.id("adminPassword"));
password.sendKeys("Derik123");
WebElement repassword=driver.findElement(By.id("adminPassword2"));
repassword.sendKeys("Derik123");
driver.findElement(By.xpath("//html/body/div[2]/div/div/div/div/div[2]/div/div/table/tbody/tr/td/table/tbody/tr/td[2]/em/button")).click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);*/
Please note that in code I commented some input field filling, because I thought first I will make it working just for 1st field. Please help me how to proceed.
the problem is after clicking the pop-up button, I'm not sure if the control switches to pop-up window or not. the gettitle after pop-up gives the main window title. and it is not able to find the first input field using the xpath or id.
The term window in Selenium means the actual browser window, not frames or just divs. Therefore, your logic is wrong, getWindowHandles() and driver.switchTo().window("Add New User") are not what you are after.
What you need is driver.switchTo().frame().
driver.switchTo().defaultContent(); // optional, use only if driver is already in an iframe
WebElement editUserForm = driver.findElement(By.cssSelector("iframe[src*='editUserForm']"));
// there are other overloads (by frame name, index, id) and locators can be used here.
driver.switchTo().frame(editUserForm);
// make sure your locator here is correct
WebElement lastName = driver.findElement(By.id("userDetailsBean.lastName")); // I doubt this is correct
// from your screenshot, I'd suggest By.cssSelector("[id*='userDetailsBean.lastName'] input")
lastName.sendKeys("harper");
Also just a kindly heads up, your code smells.
Your implicitlyWait wait usage seems wrong, please read the
documentation and the post.
Where did you get selenium.type("userDetailsBean.firstName","alan1");? Did you just copy the line from somewhere else? This looks like Selenium RC to me.
Why driver.switchTo().alert()? Have you read the documentation on what's this for?
Please don't use absolute xpath.
Try use page object if you haven't done that in your real project. (It's fine if you just want to illustrate the problem here)

Categories

Resources