Will anyone please provide how to write selenium testing code for login popup window for example like in paytm site. When click on login button, it opens a popup window. In that we have to write code to enter username and password and then login.
6 Answers
Try the Selenium IDE
- Record your steps
- Open web-page
- Click login button
- Select window
- Fill in fields
- Submit
- Export recording as code to Java or Python
Also read other questions like "How does one get started with web test automation using Selenium?" or Google for some training video's to get you started.
Update Latest version of Selenium IDE does not support exporting, check this: How to export test cases in the latest selenium IDE
Most of the popup are iframe. Hence you need to switch to the login iframe in order to enter the username and passwords as per your requirements.
Please try using following driver.SwitchTo().Frame(FrameName);
Where Framename can be 0 - 1 and so on..
You can do this by following
click on the login button
use the command -> d.switchTo().activeElement();
Here 'd' is the Webdriver instance
Also you may check if there is any frame in the pop up or not. Because you have to switch to the pop up and then iframe.
Hope it helped :)
-
Does this really help the person asking the question? Is this really what s/he was looking for? First you should be asking for clarification as to what exactly did they try out and where they are stuck? Jul 10, 2015 at 17:16
Try this! Use paytm URL instead of linkedin
@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;
-
Why are you using a sleep instead of an explicit wait for By.id("email")? The page could be faster then 2 seconds. Now you are waiting for no reasons, or it could be just a bit slower, now the test fails. This could lead to a flickering test. Oct 23, 2015 at 12:40
-
@NielsvanReijmersdal Thanks for the suggestion! Can you elaborate a bit more how can i modify my code to use explicit wait?– DishaOct 23, 2015 at 13:01
-
stackoverflow.com/questions/30986604/… and sqa.stackexchange.com/questions/12583/… Oct 23, 2015 at 13:03
Below code snippet works for me :-
Set <String> handles =driver.getWindowHandles();
Iterator<String> it = handles.iterator();
String parent = it.next();
String child = it.next();
driver.switchTo().window(child);
//perform actions on child window
//perform actions on child window
driver.close(); // only for child wondow
driver.switchTo().window(parent);
//perform actions on parent window
//perform actions on parent window
driver.quit(); // After execution of main thread and for parent window
Agreed with above steps. You will also need to add script to handle the pop-up, drive switch. Add driver switch before steps
for pop-up window. driver.switchTo().window(window name")
This will help you: http://santoshsarmajv.blogspot.com/2012/04/how-to-switch-control-to-pop-up-window.html