10

Is it possible to make Selenium WebDriver executable file in java? I have written code in java for data driven testing using Selenium WebDriver. I want to make it executable file so that outside eclipse one can execute it.

package pkg;

import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class LogingS {
  private WebDriver driver;
  private String baseUrl;

  @Before
  public void setUp() throws Exception {
    driver = new FirefoxDriver();
    baseUrl = "http://www.example.com/";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

  @Test
  public void testLogingS() throws Exception {
    driver.get(baseUrl);
    System.out.println("The current Url: " + driver.getCurrentUrl());
    driver.findElement(By.id("id_email")).clear();
    driver.findElement(By.id("id_email")).sendKeys("com");
    Thread.sleep(1000);
    driver.findElement(By.id("id_password")).clear();
    driver.findElement(By.id("id_password")).sendKeys("123");
    Thread.sleep(1000);
    System.out.println("The current Url: " + driver.getCurrentUrl());
    driver.findElement(By.id("btn_login")).submit();
    Thread.sleep(5000);
    System.out.println("The current Url: " + driver.getCurrentUrl());

  }

  @After
  public void tearDown() throws Exception {

  }
}
Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
ShutterSoul
  • 2,551
  • 3
  • 23
  • 28

7 Answers7

1

To create a new JAR file in the workbench:

  1. Either from the context menu or from the menu bar's File menu, select Export.

  2. Expand the Java node and select JAR file. Click Next.

  3. In the JAR File Specification page, select the resources that you want to export in the Select the resources to export field.

  4. In the Select the export destination field, either type or click Browse to select a location for the JAR file and Finish

  5. Open CMD, move upto your .jar

  6. Call jar file using following command :- java -jar xxx.jar

ADyson
  • 57,178
  • 14
  • 51
  • 63
0

Have you tried using ant script to run your test cases, I assume you have written your test case using Junit.

Thanks!

rsudha
  • 289
  • 1
  • 4
  • 12
0

I would suggest you to create a runnable jar of your selenium test project so that you will be able to execute it outside Eclipse IDE.

These links will help you:
Make executable jar file of webdriver project
Create runnable jar file for selenium project blog post
Making jar file of a test project in selenium webdriver

Community
  • 1
  • 1
Abhijeet Vaikar
  • 1,578
  • 4
  • 27
  • 50
  • As i know that to create jar file it shoild have main method.Bt in my selenium code there is no main method. and the link(1st link) u hav sent taht also does not hav main method. – ShutterSoul Mar 13 '14 at 10:40
  • Incase of runnable jar, you can include a class with a main method and execute junit tests programmatically. Refer to this [link](http://stackoverflow.com/questions/10127052/how-to-programmatically-execute-a-test-suite-using-junit4). – Abhijeet Vaikar Mar 13 '14 at 10:57
0

If you are test cases using jUnits then it is very simple to execute them in Eclipse. But before that you need to execute the core Selenium classes which will come to your help very frequently.

  • org.openqa.selenium.WebDriver: The main interface to use for testing, which represents an idealised web browser. The methods in this class fall into three categories – Control of the browser itself, Selection of WebElements, Debugging aids
  • org.openqa.selenium.WebElement: Represents an HTML element. Generally, all interesting operations to do with interacting with a page will be performed through this interface.
  • org.openqa.selenium.By: Mechanism used to locate elements within a document.

Below is a sample class which illustrates a Selenium test case.

import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;

import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;

public class TestEndToEndPages {

    private WebDriver driver;

    @Before
    public void setUp() {
        // Create a new instance of the html unit driver
        driver = new HtmlUnitDriver();

        //Navigate to desired web page
        driver.get("http://localhost:6060/WebApplication4Selenium");
    }

    @Test
    public void shouldBeAbleEnterUserNameAndClickSubmitToVerifyWelcomeMessage() 
    {
        // verify title of index page
        verifyTitle("Enter your name");

        //verify header of index page
        verifyHeaderMessage("Please enter your name");

        //enter user name as Allen
        enterUserName("Allen");

        //verify title of welcome page
        verifyTitle("Welcome");

        //verify header of welcome page
        verifyHeaderMessage("Welcome Allen!!!");

        //verify back link and click on it
        backToPreviousPage("go back");  

        //verify title of index page again to make sure link is working
        verifyTitle("Enter your name");
    }

    private void verifyTitle(String expectedTitle) {
        //get the title of the page
        String actualTitle = driver.getTitle();

        // verify title
        assertThat(actualTitle, equalTo(expectedTitle));
    }

    private void verifyHeaderMessage(String expectedHeaderMessage) {
        // find header element
        WebElement element = driver.findElement(By.tagName("h3"));

        String actualHeaderMessage = element.getText();

        // verify header text
        assertThat(actualHeaderMessage, equalTo(expectedHeaderMessage));
    }

    private void enterUserName(String userName) {
        // find the input text box
        WebElement element = driver.findElement(By.name("userName"));

        // set the user name in input text box
        element.sendKeys(userName);

        // submit form
        element.submit();
    }

    private void backToPreviousPage(String expectedLinkText) {
        // find the link by its id
        WebElement element = driver.findElement(By.id("back"));

        //get the actual link text
        String actualLinkText = element.getText(); 

        //verify link text with expected like text
        assertThat(actualLinkText, equalTo(expectedLinkText));

        // click the link
        element.click();
    }
}

Source: Test your web application’s UI with JUnit and Selenium

If you look closely at the comments in the above mentioned test class, you will be able to find how you can navigate to a page or how you can find an element to perform certain operations like get the text, set a value, trigger any event, etc.

UPDATE: Creating Executable JAR file.

Since you have your working JUnit test cases in place, you can use below snippet of main method and create an executable JAR file.

public static void main(String[] args) throws Exception {                    
       JUnitCore.main("foo.bar.MyTestSuite");            
}

Hope this makes for what you were looking for.

Shishir

Shishir Kumar
  • 7,981
  • 3
  • 29
  • 45
0

Yes, you can. You just need to export as executable jar file. If you want to make it fully independent, just include all lib and property files in a jar.

Here is the way in Eclipse: File/Export/Java/Runnable JAR file, after which select launch configuration and what else do you need to include in a jar.

Vardges
  • 197
  • 3
  • 8
  • 21
0

By chance I have some code I wrote that already does this using maven which works well as an example. I uploaded it to github for you. See https://github.com/johndeverall/executablejarseleniumwebdriverdemo

If you run package.bat (in a windows environment) or follow the instructions in the README.md file, maven will build you an executable jar demonstrating selenium webdriver usage.

Read the pom.xml to discover how it works.

You will need to set up maven on your machine for this (if it is not setup already) but this is trivial and well worth doing in any case.

John Deverall
  • 5,954
  • 3
  • 27
  • 37
0

export it as jar file, Save it locally , Create a batch file and call the jar to execute your script.

Syed
  • 189
  • 1
  • 5
  • 11