Monday, August 22, 2011

Start a maven project for selenium

Create a new Maven project using eclipse. The "artifactId" given for the project will be the name of the project.
Now a a new project is created. It will be having the following two directory structures.

All classes for the project will be under src/main/java  and all unit tests will be under  src/test/java.

For a selenium project there will be no need for both these folder. So we can omit src/test/java and add all tests to src/main/java. The pom file should be updated with the entry  <testSourceDirectory>${basedir}/src/main/java</testSourceDirectory> to point that all tests are in folder structure src/main/java. If only configured like this,  the surefire plugin will execute the tests.

User should specify the class which needs to be executed. It can be specified using "test" option of surefire plugin.
So if all testng tests are written in the file BaseFile.java. Then use this command from cmd mvn test -Dtest=BaseFile.java to run the test. Else the class which needs to be executed can be specified in a suite file and the "suiteXmlFiles" option of surefire  be used.

eg :
      <suiteXmlFiles>
             <suiteXmlFile>sample.xml</suiteXmlFile>
       </suiteXmlFiles>

Use "reportsDirectory"   option of surefire plugin to create a custom folder to store the emailable report.

eg:
     <reportsDirectory>${directory}</reportsDirectory>

Where 'directory' is the argument passed from cmd.

Saturday, August 20, 2011

Run tests using Maven

By default mvn test, tries to execute the unit tests in the folder src/test/java. In order to override that behavior the parameter "testSourceDirectory" can be provided.  If "testSourceDirectory" is specified as src/main/java, then the classes under that directory can be executed using mvn test command.

Tuesday, August 2, 2011

Javascript and WebDriver

It is possible using webdriver to handle javascript. For this it uses interface JavascriptExecutor.

1. Sample code for getting page title

WebDriver driver = new FirefoxDriver();
JavascriptExecutor js =  (JavascriptExecutor)driver;
js.executeScript("return document.title");


2. Executing a javascript function declared.

Sample Page Code

<script>
function showAlert(){
alert('success');
}
</script>


Sample code

WebDriver driver = new FirefoxDriver();
JavascriptExecutor js =  (JavascriptExecutor)driver;
js.executeScript("showAlert()");
On executing above code, the alert will be shown.