Giter VIP home page Giter VIP logo

selenium-cucumber-java's Introduction

Getting Started with Cucumber and CrossBrowserTesting

For this document, we provide an example test located in our Cucumber Java Github Repository.

Want a powerful and easy to use command line tool for running Selenium tests? Cucumber might be the option for you. Cucumber provides language-bindings for the powerful browser-driving tool Selenium. Its Gherkin language allows you to write your tests in a way that can be easily read by anyone on your team. Cucumber Java integrates easily with the CrossBrowserTesting platform, so you can perform tests on a wide variety of OS/Device/Browser combinations, all from one test.

Let's walk through getting setup.

1. Start be installing Maven, a software project management and comprehension tool.

2. Create a simple project with Cucumber installed  by running command:


mvn archetype:generate \
-DarchetypeGroupId=io.cucumber \
-DarchetypeArtifactId=cucumber-archetype \
-DarchetypeVersion=4.2.6.1 \
-DgroupId=seleniumcucumber \
-DartifactId=seleniumcucumber \
-Dpackage=seleniumcucumber \
-Dversion=1.0.0-SNAPSHOT \
-DinteractiveMode=false

3. After changing into the seleniumcucumber directory that was just created, add the following dependency to the pom.xml file:

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

4. Create an empty file called src/test/resources/seleniumcucumber/todo.feature with the following content:

Feature: ToDo
  Scenario: Archiving ToDos
    Given I go to my ToDo App
    When I archive all todos
    Then I should have no todos

5. Edit the file called src/test/java/seleniumcucumber/Stepdefs.java with the following content:

package seleniumcucumber;

import cucumber.api.PendingException;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.When;
import cucumber.api.java.en.Then;
import cucumber.api.java.After;
import cucumber.api.java.Before;
import org.junit.Assert.*;
import org.openqa.selenium.remote.*;
import org.openqa.selenium.*;
import java.net.URL;
import java.util.List;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Stepdefs {
private String username, authkey;
private RemoteWebDriver driver;

@Before
public void setUp() throws Throwable {

username = "YOUR_USERNAME"; //replace with your username
authkey = "YOUR_AUTHKEY"; //replace with your authkey

String browser = System.getProperty("browser");

DesiredCapabilities caps = new DesiredCapabilities();

caps.setCapability("name", "selenium cucumber");
caps.setCapability("browserName", "Chrome");
caps.setCapability("platform", "Windows 10");
caps.setCapability("screenResolution", "1366x768");
caps.setCapability("record_video", "true");

driver = new RemoteWebDriver(new URL("http://" + username + ":" + authkey +"@hub.crossbrowsertesting.com:80/wd/hub"), caps);
}
@Given("I go to my ToDo App")
public void I_go_to_my_todo_app() throws Throwable {
driver.get("http://crossbrowsertesting.github.io/todo-app.html");
}

@When("I archive all todos")
public void I_click_on_my_todos() throws Throwable {
driver.findElement(By.name("todo-1")).click();
driver.findElement(By.name("todo-2")).click();
driver.findElement(By.name("todo-3")).click();
driver.findElement(By.name("todo-4")).click();
driver.findElement(By.name("todo-5")).click();
driver.findElement(By.linkText("archive")).click();
}

@Then("I should have no todos")
public void I_should_have_no_todos() throws Throwable {
List elems = driver.findElements(By.className("done-true"));
assertEquals(0, elems.size());
}

@After
public void teardown() {
if (driver != null) {
driver.quit();
}
}

}

You’ll need to use your Username and Authkey to run your tests on CrossBrowserTesting. To get yours, sign up for a free trial or purchase a plan.

6. Now you are ready to run your test using the command:
mvn test
As you can probably make out from our test, we visit a small ToDo App example, interact with our page, and use assertions to verify that the changes we've made are actually reflected in our app.

We kept it short and sweet for our purposes, but there is so much more you can do with Cucumber! Being built on top of Selenium means the sky is the limit as far as what you can do. If you have any questions or concerns, feel free to get in touch.

selenium-cucumber-java's People

Contributors

daphnemcrossbrowser avatar

Stargazers

 avatar

Watchers

Tony Colston avatar James Cloos avatar  avatar  avatar

selenium-cucumber-java's Issues

There are a few issues with Stepdefs.java

Import of the static members needs to include the static keyword (import static org.junit.Assert.*;). Wouldn't build for me without changing this.

Also failed to connect - need to replace "@" in username:

driver = new RemoteWebDriver(new URL("http://" + username.replaceAll("@", "%40") + ":" + authkey +"@" + hub), caps);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.