Showing posts with label JUnit. Show all posts
Showing posts with label JUnit. Show all posts

Wednesday, April 22, 2015

Robotium : How to automate an Android Application using Record and playback approach

In our last post we see how to install Robotium on our Eclipse IDE. In this post we will explain on how to record an android application and playback the script.

Step 1 : Open Eclipse IDE. Before started recording we need to run the Android virtual device. Click on the Android Virtual device icon.

image

AVD manager window is displayed and start a device that is already configured. How to configure a Virtual device will be posted later in separate post. Select a device and click on Start button.

image

Launch Options window is displayed and click on Launch button.

image

Virtual device will start for Launching.

image

image

Now minimize the virtual device window, close AVD window and proceed for Robotium recording.

Step 2 : Go to File > New > Other

image

Step 3 : Click on Android – Robotium Recorder from New Window. Select New Robotium Test.

Step 4 : Click on Next button.

image 

Step 5 : A pop up dialog is displayed with the message “Please Select the root Java JDK folder”. This message is only displayed if JDK folder is not defined and configured. Click on OK button.

image

Step 6 : Java JDK window is displayed. Locate the JDK folder on the machine. Click Ok button.

image

Step 7 : Robotium Recorder window is displayed and select the apk file. In our case we have downloaded android calculator apk file from this location. Select the apk file and enter the project name. Click on Next button.

image

Step 8 : Click on New Robotium Test button.

image 

Step 9 : Recording Started. Wait for the application starts at Virtual device.

image

image

Step 10 : Go to Virtual device window and note that application will start on that window.

Step 11 : Enter 4 at First text box

Step 12 : Click on the second text box and enter 5 at second text box

Step 13 : Click on Multiply button.

Step 14 : Result will be displayed at Result text box.

Step 15 : In order to verify the result click on the Result text box. It will create an assert script.

image 

Step 16 : Note that steps are written at plain text and click on Stop recording button.

image

A window may appear and click on OK button.

image 

Step 17 : Click on Save button

image

Step 18 : A window is displayed to ask for Test Case name. Enter Test Case name as “Multiplication” and click on OK button.

image

Step 19 : Click on Finish button.

image

Step 20 : Code will be generated for this script at Eclipse editor. Expand the project of the package explorer window.

image

Step 21 : Double click on the class name “Multiplication.java”. We see the step by step script on the code. We will explain about code on future post.

image 

Step 22 : Now Run the script. Right click on the Script name and click Run as > Android JUnit test.

image

Step 23 : Go to Virtual device window and inspect that script will run and execute test.

image image

Step 24 : Result is displayed at JUnit Editor.

image

That’s all for now. Our next Robotium related post, we will show on how to refactor the Robotium script and write more manageable test.

Happy Automation day with Robotium …!!!

Monday, October 17, 2011

How to run Selenium WebDriver code with JUnit

In my previous post I have shown how to run Selenium WebDriver Code with NUnit.

Now I will show you how Selenium WebDriver code generated from selenium IDE can be run at JUnit4 Framework.

Selenium IDE has a functionality to convert its recorded script to Selenium WebDriver and Java in JUnit4 Framework.

So If we know how to record Selenium IDE (see the post : http://bit.ly/pa1TG5) then we can easily convert it to selenium WebDriver code.

Before starting we just define a Test case.

Test case :

    1. Open Selenium Website (http://seleniumhq.org/) in Firefox browser.
    2. Click Project tab
    3. Click Selenium IDE link
    4. Verify that Selenium IDE Text is present

Step 1: Record the test case in Selenium IDE

  • Open Firefox Browser
  • Open Selenium IDE window by Clicking Tools->Selenium IDE
  • Make Red colored Record button on.
  • Enter the URL : http://seleniumhq.org/ at the address bar and click Search button
  • Click Project tab
  • Click Selenium IDE link
  • Verify that Selenium IDE Text is present

The recorded script will be like the following figure

se9_thumb

Now make the record button off and Click Run button to run the script.

Step 2: Convert the Selenium IDE recorded script to JUnit 4 (WebDriver) code

  • Go to Selenium IDE window
  • Click Options menu
  • Click Format –> JUnit 4 (WebDriver)

se27

Sometimes in Options –> Format menu, JUnit 4 (WebDriver) options may not be displayed. The menu options like:

se13_thumb1

In that case, there is some change needed in selenium IDE option window. Please follow the steps:

  1. Click Options menu in Selenium IDE window
  2. Click “Options…” and Selenium IDE options window is displayed.

se12_thumb

se14_thumb1

Click Enable experimental features check box and click OK button. Now go to Format options and driver list.

  • A message dialog is displayed after selecting the appropriate format like (JUnit 4 (WebDriver))

se11_thumb

  • Click OK button
  • Selenium IDE window shows the selenium WebDriver Code in Java format with unit testing Framework JUnit 4 is implemented.

se28

 

Step 2: Run the Code in Eclipse IDE

  • Open Eclipse IDE
  • Create a New Java project by Clicking the options File –>New –>Java Project and New Java Project Window is displayed

se29

  • Enter Project name and select Project execution environment as JavaSE-1.6
  • Click Next button
  • Click Finish button
  • New Project is created
  • Add a New Java Class to the project
  • Now Copy the whole text from Selenium IDE and paste into New Java class file. It is like Copy the whole text like CTRL+A to select the whole text and CTRL+C to copy. 
  • There are some Reference error show at editor.
  • Right click on the project name and click Properties and Properties window is displayed

se30

  • Click Java Build path and click Libraries Tab
  • Click Add Library button

se31

  • Add Library window is displayed
  • Select JUnit and click Next
  • Select JUnit4 as JUnit Library and click Finish button
  • JUnit4 library is added and click OK button of properties window
  • Now go to selenium download page ( http://seleniumhq.org/download/ ) and download the latest version of Selenium Client Driver for Java.  Here version 2.8.0 is downloaded.

se33

  • Now unzip downloaded zip file and rename the folder as “Selenium” and placed to the Current project folder
  • Right click on the Project name in Eclipse and click Properties and properties window is displayed
  • Click Java Build Path
  • Go to Libraries tab and click Add External JARs

se34

  • Go to Selenium folder and select the jar file named “selenium-java-2.8.0.jar” and click Open. Selenium webdriver jar file is added

se35

  • Click OK button of Properties window
  • Remove the package name “package com.example.tests;”
  • Rename the Class name Untitled to Java Class file name

  • Add the following code at the testUntitled() method
Assert.assertEquals("Text Found", "Selenium IDE", driver.findElement(By.xpath("//div[@id='mainContent']/table/tbody/tr/td/p/b")).toString());


  • The method will be
public void testUntitled() throws Exception {
    driver.get("http://seleniumhq.org/");
    driver.findElement(By.linkText("Projects")).click();
    driver.findElement(By.linkText("Selenium IDE")).click();
    Assert.assertEquals("Text Found", "Selenium IDE", driver.findElement(By.xpath("//div[@id='mainContent']/table/tbody/tr/td/p/b")).toString());
  }
 


  • The whole codes are

  1: 
  2: 
  3: import java.util.regex.Pattern;
  4: import java.util.concurrent.TimeUnit;
  5: 
  6: import junit.framework.Assert;
  7: import junit.framework.AssertionFailedError;
  8: 
  9: import org.junit.*;
 10: import static org.junit.Assert.*;
 11: import static org.hamcrest.CoreMatchers.*;
 12: import org.openqa.selenium.*;
 13: import org.openqa.selenium.firefox.FirefoxDriver;
 14: import org.openqa.selenium.support.ui.Select;
 15: 
 16: public class testSel {
 17:   private WebDriver driver;
 18:   private String baseUrl="";
 19:   private StringBuffer verificationErrors = new StringBuffer();
 20:   @Before
 21:   public void setUp() throws Exception {
 22:     driver = new FirefoxDriver();
 23:     driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
 24:   }
 25: 
 26:   @Test
 27:   public void testUntitled() throws Exception {
 28:     driver.get("http://seleniumhq.org/");
 29:     driver.findElement(By.linkText("Projects")).click();
 30:     driver.findElement(By.linkText("Selenium IDE")).click();
 31:     Assert.assertEquals("Text Found", "Selenium IDE", driver.findElement(By.xpath("//div[@id='mainContent']/table/tbody/tr/td/p/b")).toString());
 32:   }
 33:   @After
 34:   public void tearDown() throws Exception {
 35:     driver.quit();
 36:     String verificationErrorString = verificationErrors.toString();
 37:     if (!"".equals(verificationErrorString)) {
 38:       fail(verificationErrorString);
 39:     }
 40:   }
 41: 
 42:   private boolean isElementPresent(By by) {
 43:     try {
 44:       driver.findElement(by);
 45:       return true;
 46:     } catch (NoSuchElementException e) {
 47:       return false;
 48:     }
 49:   }
 50: }
 51: 


  • Now Run project by click Run button from JUnit4

se34



  • Firefox Browser is opened and Script runs and Test is successful.

That’s all. I have described here all the steps in a very easy and details way. If any thing could not be understand, please comment me, I must answer. Please comment me about the whole blog.

Cypress: How to handle browser-based authentication pop up dialog in Cypress

Five years ago I have written a blog on how to handle browser-based authentication for selenium webdriver.   Now it is for cypress. Cypress...