Showing posts with label eclipse. Show all posts
Showing posts with label eclipse. Show all posts

Sunday, September 1, 2013

WebDriver with Maven

Maven is a build Automation Tool somewhat like ANT. Having use ANT for the previous project wanted to try Maven this time. I thought it would be easy as I know ANT, however it took a while for me to figure out as i didn't find any proper tutorial on net. And this is the reason I am writing new Blogpost.

I have just started, So if someone knows the better way to do few things mention here please let me know in comment.

Now let's try setting up new Maven Project for Webdriver using Eclipse. However before you do, you need to download and setup Maven on you local machine.

Setup up Maven
  • download the appropriate maven from the following site.
    http://maven.apache.org/download.cgi Extract it to some location and setup the PATH variable. 
  • If you have done everything correctly, Following command on CMD will let you know the Maven Version.
    mvn --version 


Install Maven Plug-in in Eclipse 
Install the m2Eclipse plugin in you eclipse

Setup Maven Project for WebDriver.

  • In Eclipse choose New Maven Project and check the "Create Simple Project" 
  • Fill the Group id and Artifact id 
  • Create New Class named "GoogleTest" under "src/test/java" Copy Following code in it.
  • GoogleTest.java 

    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.testng.annotations.Test;
    
    /**
     * @author Gaurang_Shah
     */
    public class GoogleTest {
     private WebDriver driver;
    
     @Test
     public void verifySearch() {
      driver = new FirefoxDriver();
      driver.get("http://www.google.com/");
      driver.quit();
     }
    }


  • It will show you some compile time error as we haven't added the dependencies yet.
  • Maven mention all the dependencies in pom.xml file. Please copy and replace the following pom.xml with your project pom.xml file.
    <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/xsd/maven-4.0.0.xsd">
     <modelVersion>4.0.0</modelVersion>
     <groupId>MavenWebDriverDemo</groupId>
     <artifactId>MavenWebDriverDemo</artifactId>
     <version>0.0.1-SNAPSHOT</version>
    
     <dependencies>
      <dependency>
       <groupId>org.seleniumhq.selenium</groupId>
       <artifactId>selenium-java</artifactId>
       <version>2.33.0</version>
      </dependency>
      <dependency>
       <groupId>org.seleniumhq.selenium</groupId>
       <artifactId>selenium-server</artifactId>
       <version>2.33.0</version>
      </dependency>
      <dependency>
       <groupId>org.testng</groupId>
       <artifactId>testng</artifactId>
       <version>6.8.5</version>
      </dependency>
     </dependencies>
    </project>
  • Running Project
    • To run from Eclipse, Right click on pom.xml and choose Run as --> Maven Test 
    • To run from command line, Go to project directory and enter following command
       mvn test

    Wednesday, May 13, 2009

    TestNG with Eclipse

    After exploring both JUnit and TestNG, I finally decided to go with the TestNG. And I moved on to eclipse IDE aswell, as it will provide ease in writing and running the testcases ( as compared to the dos prompt and notepad). The following blog will show you how to install TestNG plug-in in eclipse and how to create and run testcase.
    Install TestNg on Eclipse
    Create Testcase
    Run the Testcase


    Installing TestNG on Eclipse
    follow the simple steps below to install the TestNG plug-in on the eclipse IDE.
    • Open the Install/Update window from Help->Software Updates->Find and Install
    • Click on the radio button "Search for new feature to install" and click "Next" button
    • Click on the "New remote site..." and supply name as "TestNG" and URL as "http://beust.com/eclipse" and click on "OK"
    • Make sure the TestNG is checked and click on the "Finish" Button
    • Now follow the other steps and TestNG will be installed.




    Create TestCase
    To create the testcase in the eclipse you also need to create a project and import some require JAR files. So let's being with that.
    • Create the project
      Now to begin with the the eclipse you need to first create the project. it will contain all the packages and packages will contain all necessary Java files (testcases ) and configuration files ( xml files )
      So just navigate to the file->new->Java Project. Give it some appropriate name and create it.
    • Import required JAR files
      Now to support the testNG framework and selenium API you need to import their jar files.
      So just click on the Libraries tab and then click on the "Add External Jars.." import the following libraries from you hard disk.
      selenium-java-client-driver.jar
      selenium-server.jar
      testng-5.9-jdk15.jar

      If you have already created the project. Just open the project properties dialog box from Project->Properties.
      Now click on the "Java Build Path" and then click on the "Libraries" tab and import all the libraries mentioned above.

    • Write testcase.
      Now we need to create a new class file and need to write down the testcase. Just Right click on your project and click on the new->Class. Provide the name as google in the dialog box.
      Now just replace the code the Google.java file with the below code.
    Google.java
    import static org.testng.AssertJUnit.*;
    import org.testng.annotations.*;
    import com.thoughtworks.selenium.*;

    public class Google {
    private Selenium selenium;

    @BeforeClass
    public void startSelenium() {
    selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://www.google.com");
    selenium.start();
    selenium.open("http://www.google.com");
    }

    @AfterClass(alwaysRun=true)
    public void stopSelenium() {
    this.selenium.stop();
    }

    @Test(groups="search")
    public void googling() {
    try {
    selenium.type("name=q", "qtp-help.blogspot.com");
    selenium.click("name=btnG");
    selenium.waitForPageToLoad("60000");
    assertTrue(selenium.isTextPresent("qtp-help.blogspot.com"));

    } catch (SeleniumException e) {
    fail(e.getMessage());
    }
    }
    }


    Run the Testcases.
    Now you have created the testcases. But before we run it. Let's start the selenium server.
    Now to run the testcase right click on the project and then click on the Run As->Open Run Dialog box
    Now right click on the TestNg in the left pannel and click "New". Now you need to create a run configuration based on how you would like to run you testcases(s).

    Here you will find five option to run your testcases(s). Let's see one by one what all these options are.
    First browse your project by clicking on the browse button.

    Class: If you will select this, all the testcases under the selected class will run on b y one.

    Method: Choose this option if you want only particular testcase to run.


    Group: This option will allows you to run the Groups of the testcases if you have mention that in your class. You can browse all the Groups in the class and can choose particular group to run.


    For All the above option a temporary config will be created in you project folder.
    Package:Choose this option if you want to run all the classes inside a package.
    Suite: Use this option if you want to run any of the testsuit from your class. However to use this you need to create the TestNG configuration file.