Showing posts with label maven. Show all posts
Showing posts with label maven. Show all posts

Tuesday, 20 March 2012

Example: Integration of Selenium 2 with Maven

Aim:

Difficult Level: Medium

Prerequisits:
Steps to Implementation:
  1. Create the project directory "MavenProject".
  2. Create subdirectory "src".
  3. Create two subdirectory "testng" and "script" into "src" directory.
  4. Save the all below files to corresponding directory.
  5. Run the script use command: mvn install
Scripts:
  • Create file "pom.xml" under "MavenProject" directory.

<?xml version="1.0" encoding="UTF-8"?>
<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>org.time2test</groupId>
<artifactId>Time2Text</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<name>Selenium Functional Tests</name>
<url>http://www.time2test.com/</url>


 <repositories>
<repository>
<id>openqa</id>
<name>OpenQA Repository</name>
<url>http://nexus.openqa.org/content/repositories/releases/</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</repository>
</repositories>

<dependencies>

<dependency>
 <groupId>junit</groupId>
 <artifactId>junit</artifactId>
 <version>4.4</version>        
</dependency>

 <dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.6.0</version>
</dependency>

   <dependency>
     <groupId>org.openqa.selenium.server</groupId>
     <artifactId>selenium-server</artifactId>
     <version>0.9.0</version>
   </dependency>

   <dependency>
     <groupId>org.seleniumhq.selenium.client-drivers</groupId>
     <artifactId>selenium-java-client-driver</artifactId>
     <version>1.0.2</version>
     <scope>test</scope>
   </dependency>

   <dependency>
     <groupId>org.testng</groupId>
     <artifactId>testng</artifactId>
     <version>5.7</version>
     <scope>test</scope>
     <classifier>jdk15</classifier>
   </dependency>
 </dependencies>

  <build>
     <testSourceDirectory>src/script</testSourceDirectory>
     <plugins>
       <!-- Compiler Plugin (support for JDK.5)-->
       <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-compiler-plugin</artifactId>
         <version>2.3.2</version>
    <executions>
           <execution>
             <goals>
               <goal>testCompile</goal>
             </goals>
           </execution>
         </executions>
         <configuration>
           <source>1.5</source>
           <target>1.5</target>
         </configuration>
       </plugin>

       <!-- Selenium Maven Plugin -->
       <plugin>
           <groupId>org.codehaus.mojo</groupId>
           <artifactId>selenium-maven-plugin</artifactId>
           <version>1.1</version>
           <configuration>
               <port>1247</port>
               <trustAllSSLCertificates>true</trustAllSSLCertificates>

           </configuration>
           <executions>
               <execution>
                   <phase>pre-integration-test</phase>
                   <goals>
                       <goal>start-server</goal>
                   </goals>
                   <configuration>
                       <background>true</background>
                   </configuration>
               </execution>
           </executions>
       </plugin>


       <!-- Surefire plugin -->
       <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-surefire-plugin</artifactId>
    <version>2.12</version>
         <executions>
           <execution>
             <phase>integration-test</phase>
             <goals>
               <goal>test</goal>
             </goals>
             <configuration>
               <skip>false</skip>
             </configuration>
           </execution>
         </executions>
         <configuration>
           <suiteXmlFiles>
             <suiteXmlFile>
               src/testng/testng.xml
             </suiteXmlFile>
           </suiteXmlFiles> 
           </configuration>
       </plugin>        
  
  </plugins>
</build>

</project>
  • Create file "testng.xml" file under "testng" directory.

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="SeleniumWebdriverTests" parallel="tests">
<test name="WebdriverScriptWithMaven">
<classes>
<class name="com.example.tests.MavenExample" /> 
</classes>
</test>
</suite>
  • Create "MavenExample.java" file under "src/com/example/tests" directory.


package com.example.tests;

import com.thoughtworks.selenium.*;
import org.testng.annotations.*;
import static org.testng.Assert.*;
import java.util.regex.Pattern;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.junit.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;

public class MavenExample  {
private WebDriver driver;
private String baseUrl="http://foundation.time2test.co.uk/wp-login.php";
private StringBuffer verificationErrors = new StringBuffer();
@BeforeMethod
public void setUp() throws Exception {
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}

@Test
public void test() throws Exception {
driver.get(baseUrl);
driver.findElement(By.id("user_login")).clear();
driver.findElement(By.id("user_login")).sendKeys("admin");
driver.findElement(By.id("user_pass")).clear();
driver.findElement(By.id("user_pass")).sendKeys("time4test");
driver.findElement(By.id("wp-submit")).click();

// Creating a new user in Word press
driver.findElement(By.linkText("Users")).click();
driver.findElement(By.cssSelector("a.button.add-new-h2")).click();
driver.findElement(By.id("user_login")).clear();
int a=new Random().nextInt(123456789);
String User_name= "testuser"+a;
driver.findElement(By.id("user_login")).sendKeys(User_name);
driver.findElement(By.id("email")).clear();
String Email_address= "testemail"+a+"@gmail.com";
driver.findElement(By.id("email")).sendKeys(Email_address);
driver.findElement(By.id("first_name")).clear();
driver.findElement(By.id("first_name")).sendKeys("Word");
driver.findElement(By.id("last_name")).clear();
driver.findElement(By.id("last_name")).sendKeys("Press");
driver.findElement(By.id("url")).clear();
driver.findElement(By.id("url")).sendKeys("http://www.test.com");
driver.findElement(By.id("pass1")).clear();
driver.findElement(By.id("pass1")).sendKeys("123-asd");
driver.findElement(By.id("pass2")).clear();
driver.findElement(By.id("pass2")).sendKeys("123-asd");
driver.findElement(By.id("addusersub")).click();
driver.findElement(By.cssSelector("input.button")).click();

}

@AfterMethod
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}

private boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}
}
Helpful Video:

Tuesday, 19 July 2011

Example - Selenium 2 with Maven Tool

Aim:
How to work on Selenium 2 with Maven tool.

Difficulty Level: High

Questions?

  1. What is Maven tool?

Exercise Execution Time: more details coming soon

Steps

  1. First download Maven from following URL: http://maven.apache.org/download.html.
  2. Unzip the downloaded folder into ‘Program Files’ directory (not mandatory).
  3. Set M2 is “C:\Program Files\apache-maven-3.0\bin” and M2_HOME is “C:\Program Files\apache-maven-3.0” as environmental variable into your system.
  4. Set the path “C:\Program Files\apache-maven-3.0\bin” into System variables.
  5. Now Create a Selenium Project directory.
  6. Put the “pom.xml” file into Project Directory.
  7. Create some Scripts and put into Project Directory.
  8. Again create a directory into Project directory and put into “testng.xml” file.
  9. Now run the ‘mvn install’ command into command prompt and it gets executed successfully.