Friday 8 March 2013

Handling Permission denied error for error using Seleium WebDriver

This summary is not available. Please click here to view the post.

Key press events using selenium RC

To handle key press events like to press page down, tab key or other keys selenium RC supports this by passing ASCII code of keys to keyDownNative function.

Syntax:

keyDownNative(String keycode);

Example:
  • For page down/scroll: keyDownNative("32"); //32 is ASCII code for space bar.
  • To press tab key: keyDownNative("9"); // 9 is ASCII code for Tab. 

Handling alert, prompt and confirmation box using Selenium RC

This article explain about how to deal with java script alert, prompt and confirmation box.

Copy below html code and save it as test.html.

<html>
<head>
<script type="text/javascript">

function show_alert()
{
alert("I am an alert box!");
}
function show_confirm()
{
var r=confirm("Press a button");
}

function show_prompt()
{
var name=prompt("Please enter your name","Harry Potter");
if (name!=null && name!="")
  {
  document.write("Hello " + name + "! How are you today?");
  }
}
</script>
</head>
</head>
<body>
<form action="">
<input type="button" onclick="show_alert()" value="Show alert box" id="alert"/>
<input type="button" onclick="show_prompt()" value="Show prompt box" id="prompt"/>
<input type="button" onclick="show_confirm()" value="Show confirm box" id="confirm"/>
<br/>
First name: <input type="text" name="firstname" id="firstname"/><br />
Last name: <input type="text" name="lastname" id="lastname"/>
</body>
</form>
</html>

Below selenium code handles these popups.

Alert Box:

Global.selenium.chooseOkOnNextConfirmation();
Global.selenium.click("alert");
System.out.println("Clicked on OK button in alert box");
String alert=Global.selenium.getAlert();//This is needed as If an confirmation is generated but you do not consume it with getConfirmation, the next Selenium action will fail.
Global.selenium.type("lastname""venkat");
Thread.sleep(10000); //just for to see actions by selenium. Not needed 

Same way you can handle both prompt and confirmation boxes.

Confirmation: 
Global.selenium.chooseOkOnNextConfirmation();
Global.selenium.click("confirm");
System.out.println("Clicked on OK button in confirm box");
String confirmation=Global.selenium.getConfirmation
Global.selenium.type("firstname""fname");
Thread.sleep(10000);//just for to see actions by selenium. Not needed

Prompt:
Global.selenium.chooseOkOnNextConfirmation();
Global.selenium.click("prompt");
System.out.println("Clicked on OK button in confirm box");
String prompt=Global.selenium.getPrompt();
Global.selenium.type("firstname""hiiii");
Thread.sleep(10000);





Tow points to remember are :

  1. Note that your first statement should be chooseOkOnNextConfirmation(), before clicking on an element which generates alert or popup
  2. GetAlert(), GetConfirmation() and GetPrompt(); commands should be issued after alert or popup has been generated, else subsequent statements will be failed.

How to shutdown selenium server?

Selenium RC Interview Questions


Below are the list of interview questions on selenium RC. Post in comments if anyone need answers for any questions.
  1. What version of Selenium RC you are using?
  2. How Selenium RC works at core components level?
  3. What all languages and browsers that Selenium RC supports?
  4. How you start selenium server?
  5. What are different Start modes of Selenium RC?
  6. When to use proxy injection mode?
  7. How you handle SSL or HTTPS using selenium RC?
  8. How to automate Flash based applications?
  9. Explain about your automation framework
  10. How to handle alerts and pop up?
  11. How to handle Modal dialog?
  12. How to handle AJAX components?
  13. How you upload a file?
  14. How you click on scroll bar button in web application?
  15. Which 3rd party tools/add-ons you use for identifying object identifiers?
  16. How you manage object repository?
  17. How to extend selenium RC for user defined functions?
  18. Is it possible to select a text and compare text?
  19. Have you ever automated Localization test cases using selenium RC?
  20. Major differences/enhancements between Selenium RC and Web driver?
  21. How to navigate to parent tag from a child tag in xpath?
  22. How to navigate to sibling node in xpath?
  23. What are major problems you encountered during automation? and you resolved?
  24. How to connect to excel sheet for test data reading?
  25. Which locator strategy is faster? Xpath or CSS why?
  26. What are different locator strategies exists?
  27. Which reported tool or component you used?
  28. How logging handled in your automation framework?
  29. What are user extension in selenium rc?
  30. How you update selenium server jar if needed?
  31. Is it possible to capture screen shot when system is turned to stand by mode?
  32. How test case dependency handled in your framework?
  33. Can selenium used to measure performance of web application?
  34. Explain getEval and runscript functions.
  35. How you handle auto complete search in selenium rc automation?

Dealing with SSL certificate error or Https using Selenium RC

As all understand about need of SSL or Https for web applications, not stressing on concept of SSL. Basically need of SSL is to securely transmit sensitive data over the web. 


With out valid SSL certificate browsers prompted users with certificate error page as browser not trusted this site and certificate is not valid. And this is common when comes to automation also. 


To deal with this error while automating using selenium, it comes with 2 options. By default Selenium takes care this with browser modes. 
Earlier version of selenium 1.0 or lower, use *iehta for IE and *chorme for firefox as browser mode. This will take care of HTTPS errors.


In latest versions of selenium 2.0 beat use regular browser modes *iexplore, *firefox. By default these modes handle Https.

Selenium RC getEval tutorial and examples.

Using selenium getEval we can achieve many tasks which are not possible to achieve normally. To achieve this  Selenium RC provided with getEval function.

Syntax:

getEval("javascript code as string"); It returns result of Java script.

Let's have simple example first, below example written using Junit but you can convert getEval script it to your preferred language as it is.


  1. public class selenium extends SeleneseTestCase{  
  2.        
  3.       @BeforeClass  
  4.       public void setup() throws Exception  
  5.       {  
  6.             selenium = new DefaultSelenium("localhost",4444,"*firefox""http://www.google.com");  
  7.             selenium.start();  
  8.       }  
  9.        
  10.       @AfterClass  
  11.       public void tearDown()  
  12.       {  
  13.             selenium.close();  
  14.             selenium.stop();  
  15.       }  
  16.       @Test  
  17.       public void testNew() throws Exception  
  18.       {  
  19.              
  20.           selenium.getEval("alert(\"hi\")");  
  21.              
  22.       }  
  23. }  




When you run this script you can see an alert "Hi".

Now testNew() function  replace with below code.


  1. public void testNew() throws Exception  
  2.       {  
  3.              
  4.             selenium.open("http://www.google.com");  
  5.             String sCount =    selenium.getEval("window.document.getElementsByTagName('input').length");  
  6.             System.out.println(sCount);  
  7.       }  
This will give output 11. 


  1. public void testNew() throws Exception  
  2.       {  
  3.              
  4.             selenium.open("http://www.google.com");  
  5.             selenium.getEval("selenium.browserbot.getCurrentWindow().moveTo(0, 0)");  
  6.             String screenWidth = selenium.getEval("selenium.browserbot.getCurrentWindow().screen.availWidth");  
  7.         String screenHeight = selenium.getEval("selenium.browserbot.getCurrentWindow().screen.availHeight");  
  8.         selenium.getEval("selenium.browserbot.getCurrentWindow().resizeTo(" + screenWidth + ", " + screenHeight + ")");  
  9.         
  10.             String txtBoxId = selenium.getEval("window.document.getElementById(\"q\").name");  
  11.             selenium.type(txtBoxId,"selenium rc");  
  12.                          
  13.       }  


Above script maximize the goole.com window and type search text in Text Box.

Identify Iframe and dealing problems with IFRAME using Selenium RC


When you choose Selenium RC for web automation one need to actually evaluate to ensure whether Selenium fit to your application technology or not. Based on need one can eitehr incldue AJAX, IFRAME, Flash etc for better user interface. Adding such technolgies cause hurdles for automation engineers. Though there are many built in functions to support for Selenium RC. But for some issues we need tweak selenium RC to overcome such problems.
 IFRAME is an HTML element is an individual component of an HTML document IFRAME is part of webpage which contain entirely different content than page. IFRAME in turn point to diffent html page. IFRAME html tag will be used for to have an IFRAME in a web page. Normally we use IFRAME to represent tree kind of structure like Page Index, Org Structure like that.


IFRAME is differnt than Modal dialog. Modal dialog should be closed before you access parent page. But with IFRAME we can access Parent window unless restricted by developer.
Let assume that in Bank application one need to select an account type in IFRAME and then check account details by administrator.
The main problem that one encounter when delaing with IFRAME is set foucus to IFRAME do some actions in IFRAME and then close IFRAME and    perform few action again in Parent window.
As I said IFRAME is independent HTML page one need to get Identifier of IFRAME. Using Internet developer tool bar FireBug one can get IFRAME id. There will mutliple IFRAME tags some times so one need to get IFRAME id of root levels.


Then use selectFrame(FrameID) of Selenium command. This will focuse the control to IFRAME. Now do some actions. Here sometime user face proble to focuse to IFRAME. In such cases first focus selenium to Parent window using selectwindow("null"). "null" is parent window id. 
So once you are done with actions in IFRAME you close the IFRAME either clicking on OK or Close button. Before you perform any action in parent window now, first retun focuse again to parent window using selectWindow("null").
Have any questions or comments post here!!
Related Posts Plugin for WordPress, Blogger...