Tuesday, October 28, 2014

Maven~izing your WebDriver/TestNG Project

Downloaded Maven by following it's instructions http://maven.apache.org/download.cgi
Installed it in Windows7 and installed it also in eclipse.

Went to my selenium workspace and created a folder for my first maven project.
Copy and pasted this command

mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

com.mycompany.app -> is the group id or package in your java project, you can change this
my-app -> is the project name

This command was pasted in command line in windows inside the maven project.
It will download the needed maven files if it's your first time installing it.

Source Packages in Maven Project
src/main/java - is where you put all your core classes and utilities
src/tset/java - is where you put your test classes like WebDriverTest

They key in Maven project of course is the pom.xml. Without POM you cannot build your maven project. But you have to make sure you right the pom properly. I learned pom.xml by following the tutorial of Software Forum Testin in youtube and below is the sample pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>
com.selenium.app</groupId>
  <artifactId>
myseleniumtests</artifactId>
  <packaging>
jar</packaging>
  <version>
1.0-SNAPSHOT</version>
  <name>
myseleniumtests</name>
  <url>
http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>
junit</groupId>
      <artifactId>
junit</artifactId>
      <version>
3.8.1</version>
      <scope>
test</scope>
    </dependency>
    <dependency>
    <groupId>
org.seleniumhq.selenium</groupId>
    <artifactId>
selenium-server</artifactId>
    <version>
2.43.1</version>
    </dependency>
    <dependency>
    <groupId>
org.testng</groupId>
    <artifactId>
testng</artifactId>
    <version>
6.8.8</version>
    </dependency>
  </dependencies>
  <build>
 <plugins>
  <plugin>
  <groupId>
org.apache.maven.plugins</groupId>
  <artifactId>
maven-compiler-plugin</artifactId>
  <version>
3.2</version>
  <configuration>
  <source>
1.6</source>
  <target>
1.6</target>
  </configuration>
  </plugin>
  <plugin>
  <groupId>
org.apache.maven.plugins</groupId>
  <artifactId>
maven-surefire-plugin</artifactId>
  <version>
2.4.2</version>
  <configuration>
  <suiteXmlFiles>
  <suiteXmlFile>
testing.xml</suiteXmlFile>
  </suiteXmlFiles>
  </configuration>
  </plugin>
  </plugins>
  </build>
</project>
As you can see, pom.xml is pretty straight forward. If you understand what the file is for you'll understand its content. This file is what is needed by maven to build the project and run it. Above are dependencies I want my build to run with including the plugins.

But I had problem identifying the versions of the plugins I'm using and had several errors in my build. The solution is found here: http://search.maven.org/#browse

You can look for the plugin name and see the version, so you can put it in your pom.xml.

Then if you encounter error, make sure you either run
mvn clean install
or update maven project via eclipse
above did the trick for me after I carelessly deleted my .m2 folder.

Monday, October 27, 2014

Selenium Training Session Notes

Selenium Training Session
by: Software Testing Forum

******************** Video 1 Notes ********************

To test xpath you can use firebug or chrome (press F12 then ESC key)
$x -> for xpath commands
$$ -> CSS Locator

Sample page or test page: http://www.wikipedia.org/
Experiment selection for input field of search and language selection dropdown

// -> signifies or shows DOM properties (read further)
select -> html tags
@ -> is used to call the properties inside the tag
For more xpath functions: http://www.w3schools.com/xpath/xpath_functions.asp

Below are xpath samples:

If the ID is static and you want to select the input field with its id
$x("//input[@id='searchInput']")

This example below shows you that id and name can be used to identifiy the input field
$x("//input[@id='searchInput'][@name='search']")

If you are only sure of the start and other data keeps on changing
or you want to select those that starts-with, below is an example
$x("//input[starts-with(@id, 'search')]")

Here's how to use a substring if you want to base your selection on the portion of the
field's name or id or other tags
$x("//input[substring(@id, 2)='earchInput']")

Here's another example on how to use a substring with ending
$x("//input[substring(@id, 2, 5)='earch']")

If you simply want to select the field with certain pattern and can't be captured
by starts-with and substring, there is also contains function
$x("//input[contains(@id, 'npu')]")

Using following-sibling function will help you select what's beside the item you
just selected if there's no id or name you can use to select it
$x("//input[contains(@id, 'npu')]/following-sibling::select")

If you want to select the parrent it belongs to you can use /parent or /..
If you've noticed for .. it's like the command line in windows, y
$x("//input[contains(@id, 'npu')]/parent::fieldset")
$x("//input[contains(@id, 'npu')]/../..")

$x("//input[contains(@id, 'npu')]/../../../following-sibling::div")
$x("//input[contains(@id, 'npu')]/../../../following-sibling::div[3]")

$x("//select[@id='searchLanguage']/preceding-sibling::input")

Below are some samples of CSS Locator
$$ ("input[id='searchInput']")
$$ ("input[id='searchInput'][name='search']")
Not all xpath codes will work if you simply remove the \\ and @ sign
you wil still have to check the proper syntx for css selector.
Above example just quickly shows you the main difference between xpath codes.

******************** Video 2 Notes ********************

Will list all span under label (we use single / because span is direct child of label)
$x("//label[@for='langsearch-input']/span")

CSS Locator
$$("div[class='langlist langlist-large']>span")
>span  is what you add if you want to list down all direct span child of label

Will return the first span child in hierarchy
$$("div[class='langlist langlist-large']>span:nth-child(1)")

works same like nth-child but just ignores the hierarchy
$$("div[class='langlist langlist-large']>span:nth-of-type(1)")

If you want to select the ID you can also use # instead of typing ID
$$("#searchInput")

If it's not a direct childd and you want to select it
(from wikepedia selenium software search result)
$$(".mw-search-results li:nth-of-type(1) a")

Download TestNG framework : http://testng.org/doc/download.html
TestNG allows panel execution of test
TestNG allows execution of failed test which is not in JUnit

testing.xml

<suite name="Wikipedia Test" verbose="3" parallel="tests">
parallel
- methods : if you want to execute per method
- tests : if you want both executed at the same time

driver.close - will free the memory
driver.quit - will quit the browser

******************** Video 5 Notes ********************

Javascript methods in test - never use it unless it's really necessary
Document Object Model (DOM)
-You can use to navigate to each elements
-returns value with same element name in an array
--document.getElementsByName('firstName')
-this is how you get the first value of an array
--document.getElementsByName('firstName')[0]
-while this will put value to your DOM
--document.getElementsByName('firstName')[0].value='selenium'
-below casts the driver to JavascriptExecutor
--((JavascriptExecutor)driver).executeScript("document.getElementsByName('firstName')[0].value='123'");
-and to pause the execution
--Thread.sleep(5000) //5 seconds

dependsOnGroup="regTest"
-- if you want your test executed and dependent on a group of methods
-- it is also included inside @Test(..., @dependsOnGroup="regTest")
-- then the method, to be part of the group should have groups="regTest" inside @Test

if you want to know the time executed per method you can use new GregorianCalendar().getTime();
you can add this before you execute anything in your method, and add it at the end of all the lines

WebDriver and AndroidDriver

This is my notes in installing WebDriver and AndroidDriver
As of the moment these are what's available
selenium-2.43.1
adt-bundle-windows-x86_64-20140702
eclipse (I'm using the one the came with adt bundle)

Followed instructions found in https://code.google.com/p/selenium/wiki/AndroidDriver

For your SDK, you can download and install any Android version that you need. I've actually downloaded several versions like Android 5.0 (API 21) down to Android 4.0 (API 14). I just need the versions for my testing purposes.

I'll be using API 19 as my emulator in running my WebDriver.

When I reached the section Run the Tests in AndroidDriver website, eclipse can't seem to find
import org.openqa.selenium.android.AndroidDriver;


Where is this class?

So I went back to selenium website http://www.seleniumhq.org/download/
to check the Javadoc and didn't find the AndroidDriver I needed. So I went to google download list and searched for all available items to download and then sorted it by date (DESC).

https://code.google.com/p/selenium/downloads/list

I've downloaded this version, selenium-java-2.39.0, which is deprecated already. I downloaded it anyway since I just want to know if the project will build without error. So I've imported the jars and successful built it.

Selenium Change Log (2.40 and up)
If you've noticed for selenium version higher than 2.39 the AndroidDriver was removed in Selenium. If you check the log, they did that so that if you want to use the android driver you'll have to use Selendroid.