Popular Posts

Wednesday, 23 February 2011

Selenium 2 testing with iPhone - Part2

How to locally setup iphonedriver/webdriver under Selenium 2?

Working with Web Driver:
WebDriver uses a different underlying framework from Selenium’s javascript Selenium-Core. It also provides an alternative API with functionality not supported in Selenium-RC. WebDriver does not depend on a javascript core embedded within the browser, therefore it is able to avoid some long-running Selenium limitations.
WebDriver’s goal is to provide an API that establishes
• A well-designed standard programming interface for web-app testing.
• Improved consistency between browsers.
• Additional functionality addressing testing problems not well-supported in Selenium 1.0.




1. Download the binary file from http://code.google.com/p/selenium/downloads/list link. Unzip the downloaded ‘selenium-java-2.0a6.zip’ file and save it into your hard disk.


• Unzipped folder contains the jar files.


• Launch Eclipse exe, if you launch first time on your box then user would see the following screen, enter your workspace location and click Ok

• Eclipse screen is following:



• Select File > New > Java Project.



• Provide Name to your project, Select JDK in ‘Use a project Specific JRE’ option (JRE6 selected in this example) > click Next then Finish.


• Keep ‘JAVA Settings’ intact in next window. Project specific libraries can be added here, this would create project Demo in Package Explorer/Navigator pane



• Right click on src folder and click on New > Folder, name this folder com and finish



• This should get com package inside src folder.



• Following the same steps create core folder inside com.



• Following the same steps create tests folder inside core


• Create a folder called lib inside project Demo. Right click on Project name > New > Folder. This is a place holder for jar files to project (i.e. Selenium client driver, selenium server etc).



• This would create lib folder in project directory.



• Right click on lib folder > Build Path >Configure build Path


• Under Library tab click on Add External Jars to navigate to directory where jar files are saved. Select the jar files which are to be added and click on Open button.


• Click on Add External jars button and add “selenium-java-client-driver.jar”, “junit.jar” and all the jar files of ‘selenium-java-2.0a6’ folder from your system. User would see following screen, now click Ok button:



• Right click on com.core.tests folder, click New>>Class


• Give a class name like into the following screen and click Finish button:


• Paste the following codes
package com.core.tests;import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.htmlunit.HtmlUnitDriver; public class Example  {public static void main(String[] args) {// Create a new instance of the html unit driver// Notice that the remainder of the code relies on the interface,// not the implementation.WebDriver driver = new HtmlUnitDriver();// And now use this to visit Googledriver.get("http://www.google.com");// Find the text input element by its nameWebElement element = driver.findElement(By.name("q"));// Enter something to search forelement.sendKeys("Cheese!");// Now submit the form. WebDriver will find the form for us from the elementelement.submit();// Check the title of the pageSystem.out.println("Page title is: " + driver.getTitle());}}




• Open a Command Prompt and go to the following location. C:\selenium-rc\selenium-server-1.0.3
Launch selenium rc server as in following screen using
java –jar selenium-server.jar


• Right click on the test, and Run >> Run Configuration


• Click on RUN button to run the test



• You would see the following screen



Example2:
• Paste the following codes
package com.core.tests;import java.util.List;import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.RenderedWebElement;import org.openqa.selenium.WebElement;import org.openqa.selenium.firefox.FirefoxDriver;public class Example {public static void main(String[] args) throws Exception {// The Firefox driver supports javascriptWebDriver driver = new FirefoxDriver();// Go to the Google Suggest home pagedriver.get("http://www.google.com/webhp?complete=1&hl=en");// Enter the query string "Cheese"WebElement query = driver.findElement(By.name("q"));query.sendKeys("Cheese");// Sleep until the div we want is visible or 5 seconds is overlong end = System.currentTimeMillis() + 5000;while (System.currentTimeMillis() < end) {// Browsers which render content (such as Firefox and IE)// return "RenderedWebElements"RenderedWebElement resultsDiv = (RenderedWebElement) driver.findElement(By.className("gac_m"));// If results have been returned,// the results are displayed in a drop down.if (resultsDiv.isDisplayed()) {break;}}// And now list the suggestionsList<WebElement> allSuggestions = driver.findElements(By.xpath("//td[@class='gac_c']"));for (WebElement suggestion : allSuggestions) {System.out.println(suggestion.getText());}}}





• Open a Command Prompt and go to the following location. C:\selenium-rc\selenium-server-1.0.3
Launch selenium rc server as in following screen using
java –jar selenium-server.jar

Run the above test
• You would see the output screen will look like following screen:



Working with iphone driver:

The iphone driver allows testing on a UIWebView on the iphone. It works through the use of an iphone application running on your iphone, iPod touch or iphone simulator.
I have compiled the following code which basically does a simple Google search.
I am assuming that above environment is already setup in your Eclipse IDE now go through the below instructions.
Next, create a file into your C drive called “url.properties” and add the following values.
com.bt.url
com.bt.auturl
• Copy the following code:
package com.core.tests;Import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.iphone.IPhoneDriver;import java.util.Properties;public class Example {public static void main(String[] args) throws Exception {Properties p = new java.util.Properties();p.load(new java.io.FileInputStream("c:\\url.properties"));String url = p.getProperty("com.bt.url");System.out.println("Connecting to iWebDriver on: " + url);WebDriver driver = new IPhoneDriver(url);driver.get(p.getProperty("com.bt.auturl"));WebElement element = driver.findElement(By.name("q"));element.sendKeys("Selenium");element.submit();// Simple outputSystem.out.println("Page title is: " + driver.getTitle() + "\n");System.out.println("Page URL is: " + driver.getCurrentUrl() +   "\n");}}


Next, create a file called “url.properties” and add the following values.
com.bt.url
com.bt.auturl
The first key defines where iWebDriver sits. The second key defines the google URL.
Finally run the following commands (UNIX Only. I guess you can use cygwin32)
See below screen:

# UNIX based systems is a hell of a lot nicer for java because you don't need to type
# in all the jar files or update classpaths. But then again its my preference.
# Compiling it
javac -cp `find . -name "*.jar" xargs sed 's/ /:/g;s/.\///g;'`:. ClassSource.java
# Running it
java -cp `find . -name "*.jar" xargs sed 's/ /:/g;s/.\///g;'`:. ClassName
output It will then run and produce some telling you about the page. You will also see it navigate through the page on the iWebDriver application (It’s open source so you can even hack away at the code there to customize it to your liking).



• Run the above example


We are facing the following red errors and need to resolve it:
Exception in thread "main" org.openqa.selenium.WebDriverException:

<HTML>
<HEAD>
<TITLE>Internal Server Error</TITLE>
</HEAD>
<BODY>
<H1>Internal Server Error - Read</H1>
The server encountered an internal error or misconfiguration and was unable tocomplete your request.
<P>Reference&#32;&#35;3&#46;b232287c&#46;1287409401&#46;3784817
</BODY>
</HTML>



Here is the link to access next blog on same topic:
http://time2testblog.blogspot.com/2011/02/selenium-2-testing-with-iphone-part3.html




Here is the embedded video file:

SeleniumWithiPhone-part2
Uploaded by vipingupta. - Explore more science and tech videos.


Jump to: What can you  learn?

4 comments:

  1. This looks like a great idea and timely for my testing, but I've got two issues. First, I don't know what your java compile and run commands are doing since I'm not Linux proficient. 2nd, I the simulator isn't, as far as I can tell, truly representing the iPhone browser experience since the browser reports itself as Mozilla 5 on Windows. An actual iPod Touch reports itself as Safari 5 on iPhoneOS. Still, I'd like to give it a try.

    Could you please offer a bit of insight into what the java commands are doing so that they can be written to run in a dos shell? I'm not proficient with the Linux command line.

    ReplyDelete
  2. Hi,

    Java files can be compiled/executed in DOS shell, here I am explaining how:

    1. Suppose we have project directory, which contains lib, bin folders
    2. Lib contains all the included jars in project
    3. Bin contains all the java files

    For compilation, below is the command:
    javac -cp C:\lib\Junit.jar;C:\lib\Junit1.jar; HelloWorld.java

    In the above command we have to define all the jars one by one having semi colon in between each, I have shown just two.

    For execution, below is the command:
    java HelloWorld.java

    Please let me know if you have any further clarification on same.

    ReplyDelete
  3. Can Selenium 2.x be used for automated testing of native applications on the iOS and Android or is limited to just testing the browser on those devices?

    ReplyDelete
  4. Hi Vipin,
    wonderful tutorial, but it's a pity it is not complete. Can you explain us how to get rid of that error message we get at the end of the tutorial?
    So far I haven't found any solution to run my test cases using windows xp, do you have any idea?
    Thanks again for the wonderful contribution

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...