-1

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.

Niels van Reijmersdal
  • 32,520
  • 4
  • 57
  • 124
Bhavya
  • 19
  • 1
  • 1

6 Answers6

2

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

Niels van Reijmersdal
  • 32,520
  • 4
  • 57
  • 124
0

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

Glorfindel
  • 314
  • 1
  • 6
  • 14
AKCho
  • 1
0

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..

0

You can do this by following

  1. click on the login button

  2. 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? – IAmMilinPatel Jul 10 '15 at 17:16
0

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; 
Disha
  • 51
  • 1
  • 2
  • 9
  • 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. – Niels van Reijmersdal Oct 23 '15 at 12:40
  • @NielsvanReijmersdal Thanks for the suggestion! Can you elaborate a bit more how can i modify my code to use explicit wait? – Disha Oct 23 '15 at 13:01
  • http://stackoverflow.com/questions/30986604/is-it-best-practice-to-use-thread-sleep-or-explicit-wait-before-click-on-any-e and http://sqa.stackexchange.com/questions/12583/is-it-a-bad-practice-to-use-implicit-wait-in-selenium-webdriver-should-one-use – Niels van Reijmersdal Oct 23 '15 at 13:03
0

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
Narendra Chandratre
  • 2,776
  • 7
  • 29
  • 60