Creating an executable file in Selenium WebDriver with Java

Is it possible to make Selenium WebDriver executable in java? I wrote code in java to test data using Selenium WebDriver. I want to make it an executable so that it can be run outside of eclipse.

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 { } } 
+8
java eclipse testing selenium-webdriver
source share
7 answers

Have you tried using ant script to run test cases, I suppose you wrote a test case using Junit.

Thanks!

0
source share

I would suggest you create a runnable jar of your selenium test project so that you can execute it outside of the Eclipse IDE.

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

0
source share

If you use test cases with jUnits, then they are very easy to execute in Eclipse. But before that, you need to run the main Selenium classes, which very often come to your aid.

  • org.openqa.selenium.WebDriver : The main testing interface, which is an idealized web browser. Methods in this class are divided into three categories: control of the browser itself, selection of WebElements, debugging tools
  • org.openqa.selenium.WebElement : Represents an HTML element. As a rule, all interesting operations related to interacting with the page will be performed through this interface.
  • org.openqa.selenium.By : a mechanism used to search for elements within a document.

The following is an example class that illustrates the 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: Check Web Application User Interface Using JUnit and Selenium

If you carefully look at the comments in the aforementioned test class, you can find how you can go to the page or how you can find an element to perform certain operations, such as receiving text, setting a value, triggering any event, etc.

UPDATE: Creating an executable JAR file.

Since you have valid JUnit test cases, you can use the main method fragment below and create an executable JAR file.

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

Hope this does what you were looking for.

Shishir

0
source share

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

The following is an example in the Eclipse file: file / export / Java / Runnable JAR, then select the launch configuration and what else needs to be added to the jar.

0
source share

By chance, I have some code that 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 (on a Windows environment) or follow the instructions in the README.md file, maven will create an executable jar showing you how to use the selenium web server.

Read the pom.xml file to find out how it works.

To do this, you need to configure maven on your computer (if it is not already configured), but this is trivial and worth it in any case.

0
source share

export it as a jar file, save it locally, create a batch file and call the jar to execute the script.

0
source share

To create a new JAR file in production:

  1. From the context menu or the File menu bar, select Export.

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

  3. On the JAR file specification page, select the resources that you want to export in the Select resources to export field.

  4. In the Select Export Destination field, enter or click Browse to select a location for the JAR file, and click Finish.

  5. Open CMD, go to your .jar

  6. Call the jar file with the following command: - java -jar xxx.jar

0
source share

All Articles