2

I am trying to automate a webpage using Single Sign-On. When you try to access the web page it redirects you to different page of Identity Provider, where it asks you for Username and Password inside HTTP Basic Auth popup. After successful authentication, it will redirect you back to originally requested page. Now I am trying to automate this using selenium web driver version 2.45.0. I am first switching to the alert box and then using Robot and Clipboard to do Ctrl+c and Ctrl+v the credentials. The test case works fine on my laptop, where Browser pops up, credentials are keyed in and final page appears after authentication.

When I am trying to run the same code on a linux host command line using Maven after setting DISPLAY=:99 environment variable (headless mode), it is getting stuck. The screenshot statements put to capture the state are displaying blank window. Any idea what can go wrong?

Relevant code snippet:

        Alert alert = driver.switchTo().alert();
        alert.sendKeys(username);
        robot.keyPress(KeyEvent.VK_TAB);
        robot.keyRelease(KeyEvent.VK_TAB);
        StringSelection pass = new StringSelection(pwd);
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(pass, null);

        if (System.getProperty("os.name").startsWith("Mac OS")) {
            robot.keyPress(KeyEvent.VK_META);
            robot.keyPress(KeyEvent.VK_V);
            robot.keyRelease(KeyEvent.VK_V);
            robot.keyRelease(KeyEvent.VK_META);
        } else {
            robot.keyPress(KeyEvent.VK_CONTROL);
            robot.keyPress(KeyEvent.VK_V);
            robot.keyRelease(KeyEvent.VK_V);
            robot.keyRelease(KeyEvent.VK_CONTROL);
        }
        File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(scrFile, new File("/tmp/screenshot2.png"));
        alert.accept();
  • 1
    Is there any JavaScript on your pages? If not you could test on HTTP/REST level instead, e.g., using REST-assured. That's obviously not a solution, but we have used this as a workaround when fixing a problem was too time-consuming. – dzieciou Aug 3 '16 at 5:50
  • Yup, we have javascript and css files on the page. – Krishna Pandey Aug 3 '16 at 5:52
  • Are black screenshots from command-line happening only in this scenario? And are we talking about screenshots taken by Selenium for the browser or taken some other way? – dzieciou Aug 3 '16 at 5:55
  • Yes, seems like alert box is not getting displayed or not getting captured. Yes, taking screenshots by selenium. Updated that code as well in question just now. – Krishna Pandey Aug 3 '16 at 6:21
  • @Krishna, Is that copy & paste is only option to use? Can't we directly enter proper credentials in pop-up itself? – NarendraC Aug 3 '16 at 17:46
3

Thanks for your attention to this post. I finally found the problem when one of my colleague suggested to use VNC to see what's actually going on the linux machine.

For debugging, we need to install "x11vnc" package on the system where tests is being run and then connect to the system using VNC Viewer on a Remote Host.

On target system, run "x11vnc -passwd secret -display :99" and connect to the target with ip_address:5900 (5900 is default port). Soon you will see firefox starting in poor resolution :)

Note: "secret" value to -passwd option can be replaced with any value of your choice. You need to enter this password when prompted by your vnc client.

In my case, I found out that basic authentication pop-up was displayed more different and the tab was sending focus to Google Search text box. Hence the password was getting entered at wrong place.

I think this is valuable technique to find out what's going wrong on your linux box, which doesn't support GUI or GNOME.

How Firefox 22 displays popup on Linux

1

You can try sending the credentials in the "Authorization" header using a special URL format like this:

[protocol][user:password]@[domain_name]

You can copy your homepage url and insert user:password@ after the //

For example: https://user:password@www.example.com

  • @lauda This doesn't work coz the authentication page is different from the requested page. – Krishna Pandey Jul 26 '16 at 3:27
  • @TESTasy that's not a link, that's an example to supply basic auth credentials as part of URL. – Krishna Pandey Jul 26 '16 at 3:29
  • 1
    Well I guess it's my mistake then... But again this makes it not a good asnwer. There should be proper reasoning to explain the example and why @lauda thinks this will solve the problem. – IAmMilinPatel Jul 26 '16 at 3:31
  • If this can work for the other authentication page you could try to add these credentials for that page and when the tests is starting to visit that page first and after that to go to the page you need, homepage or any page. – lauda Jul 26 '16 at 7:58
  • @lauda that doesn't work coz the page it is redirecting to isn't static/constant. Also how do you provide basic auth details for a webpage that isn't even loaded yet? – Krishna Pandey Aug 2 '16 at 18:43
0

There are 2 issues :

  1. Robot class is taking real screen parameter and worked on that. In headless browser there is no alert is available so it would stuck when robot try to access that alert or pop up window. Also your implicit wait is more so you feels it is stuck. But wait until your implicit wait completed. After that it will throws error.

  2. In Headless browser Selenium can not able take screenshot of what your are accessing. Reference

Actually any different kind of code will not helps you. Because any API require displayed component or screen for screenshots.

Solution : I would suggest that Try to run with real browser. Headless is better when there are not pop up windows and Ajax type calls.

  • That code works with real browser as mentioned in question itself. – Krishna Pandey Aug 4 '16 at 9:21
  • But for headless it can't be. – Sagar007 Aug 4 '16 at 9:38
  • Incidentally, if you need a headless browser that can take screenshots, look into PhantomJS. – Cronax Aug 4 '16 at 12:15
  • I think this question is based on selenium and headless browser. So my answer context in only within that. If phantomJS can help the questioner then its great. – Sagar007 Aug 4 '16 at 12:18
  • @Sagar007 I just answered my question, please take a look. It's lifesaving when debugging blind-folded. – Krishna Pandey Aug 9 '16 at 16:47
0

Update 2

Issue: the page it is redirecting to isn't static/constant and you don't know exactly what IdP Random Page coz it will be based on server config

Suggestion

  1. Setting up same account (username/password) for all redirect pages
  2. Build list of URL (e.g. https: //user:password@redirectPage1/originalURL=yoursite)
  3. Create a test for each redirectPage
    • redirectPage1 --> verify A
    • redirectPage2 --> verify B
    • ...

Update 1

I just see the your reply to @lauda that the redirected site/page is not static.

Assume there are 3 pages (page1, page2, page3) in redirection list, I think you should know the condition to redirect to each page. So you can base on this rule to build a URL1 that always redirect to page1, and so on URL2 for page2, URL3 for page3.

If my assume works then

  1. Login Identity Provider site using @lauda comment: https://user:password@www.example.com
  2. Go to your home page
  3. Continue your script

Original

Workaround

  1. Go to Identity Provider (redirect) site (use redirection link)
  2. Then login as @lauda's comment https://user:password@www.example.com
  3. Go to your home page
  4. Continue your script

Hope it helps.

  • The problem with this approach is that the first requested page redirects you to the Identity Provider page, putting the requested page as OriginalUrl parameter in the end of URL in address bar/get request. And after successful authentication, the IdP redirects you to Original URL which is part of the redirection URL. So I can't go to IdP directly. – Krishna Pandey Aug 3 '16 at 16:58
  • What happens if you use the link that included the requested page as parameter in the end of URL (go to your home page, copy URL after redirection). Does it give the same behavior ? If yes, then you can replace that link at #1 step above. – Tam Minh Aug 4 '16 at 4:27
  • @KrishnaPandey One trick is to use proxy that will replace redirect URL on the fly. – dzieciou Aug 4 '16 at 6:07
  • @Krishna Pandey: I just update the answer after seeing your reply to lauda "the page it is redirecting to isn't static/constant" – Tam Minh Aug 4 '16 at 9:06
  • So there is Page1, IdP Random Page, Page1. 1. So providing username, password to Page1 won't work. 2. You don't know exactly what IdP Random Page coz it will be based on server config, so you can't hardcode it. – Krishna Pandey Aug 4 '16 at 9:22
0
  • Solution 1:- Go with the straight forward process as do manually:

    For that we need to maintain proper flow while working with main window and pop-up. Refer below snippet, Consider main window = paren & pop-up window = child

    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
    
    driver.close(); // only for child wondow
    
    
    driver.switchTo().window(parent);
    //perform actions on parent window
    
    driver.quit(); // After execution of main thread and for parent window
    
  • Solution 2:- Work around to handle pop-up -

If you are really looking to have some way to implement, You can use "Java AWT Robot class" to handle the scenario. using this class you can perform mouse actions on basis of screen coordinates or you can use AutoIT to automate this process for handling windows popup

Update us if you have queries or issue resolved.

  • @Krishna - You have not find any answer useful? – NarendraC Aug 9 '16 at 15:57
  • Thanks for your help. The tab before pasting password using clipboard was sending focus to Google Search textbox of Firefox. I was able to debug that using VNC. See my answer for how to setup that. – Krishna Pandey Aug 9 '16 at 16:49
  • forgot to add I am yet to try the snippet above, will update once I do. – Krishna Pandey Aug 10 '16 at 6:21
  • Hope it helps you – NarendraC Aug 10 '16 at 6:37
  • I tried this, it didn't work. – Krishna Pandey Aug 24 '16 at 9:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.