Skip to content

Instantly share code, notes, and snippets.

@johnny2k
Created September 12, 2014 18:32
Show Gist options
  • Save johnny2k/834430b5392852e0a422 to your computer and use it in GitHub Desktop.
Save johnny2k/834430b5392852e0a422 to your computer and use it in GitHub Desktop.
A snippet of how I'm using cucumber and the appium java client to run iOS tests. How do I extract the step definitions into a separate class?
package client.test;
import cucumber.api.Scenario;
import cucumber.api.java.After;
import cucumber.api.java.Before;
import cucumber.api.java.en.And;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import io.appium.java_client.AppiumDriver;
import org.apache.commons.lang3.time.DateUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import javax.mail.*;
import java.io.File;
import java.io.IOException;
import java.net.*;
import java.util.*;
import java.util.concurrent.TimeUnit;
import static org.junit.Assert.*;
public class clientStepDefs {
private AppiumDriver driver;
private int defaultWait = 30;
private String randomString = "";
private boolean finished = false;
private String appVersion = System.getProperty("appVersion");
private Scenario scenario;
@Before
public void setUp(Scenario scenario) throws Throwable
{
this.scenario = scenario;
File appDir = new File(System.getProperty("user.dir"), "../ipa");
File app = new File(appDir, "otherClientclientPilot.ipa");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.VERSION, System.getProperty("version"));
capabilities.setCapability("udid", "auto");
capabilities.setCapability("platformName", "iOS");
capabilities.setCapability("deviceName", "iPhone");
capabilities.setCapability("locationServicesAuthorized", true);
capabilities.setCapability("locationServicesEnabled", true);
capabilities.setCapability("noReset", true);
capabilities.setCapability("bundleId", "com.clientinc.client");
capabilities.setCapability("autoAcceptAlerts", true);
capabilities.setCapability("app", app.getAbsolutePath());
driver = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.manage().timeouts().setScriptTimeout(30, TimeUnit.SECONDS);
}
@After
public void tearDown()
{
if(this.scenario.isFailed() && !this.scenario.getName().equals("Check agent email for new message from user")){
I_take_a_screenshot();
}
if(finished){ // Only removes the app after the last scenario has been ran.
driver.removeApp("com.clientinc.client");
}
driver.quit();
}
@Then("^I enter \"([^\"]*)\" into the search bar$")
public void I_enter_as(String text) throws Throwable {
WebElement searchBar = driver.findElement(By.className("UIASearchBar"));
searchBar.sendKeys(text);
}
@Given("^I am logged in as \"(.*?)\" with password \"(.*?)\"$")
public void I_am_logged_in_as_with_password(String user, String pass) throws Throwable {
login(user, pass);
}
/**
* Sets the MAXIMUM amount of time to wait for a step to complete
* @param delay
* @throws Throwable
*/
@Then("^I set the wait time to (\\d+) seconds$")
public void I_set_wait_time_to_seconds(int delay) throws Throwable {
driver.manage().timeouts().implicitlyWait(delay, TimeUnit.SECONDS);
defaultWait = delay;
}
/**
* Find and press an element based on the element text
* @param text
* @throws Throwable
*/
@Then(value="^I press the \"([^\"]*)\" text$", timeout=500)
public void I_press_the_text(String text) throws Throwable {
List<WebElement> items = driver.findElements(By.className("UIAStaticText"));
for(WebElement item : items) {
if (item.getText().contains(text) && item.isDisplayed()) {
item.click();
return;
}
}
fail("\"" + text + "\" was not found");
}
/**
* Find and press an element based on the element name
* @param text
* @throws Throwable
*/
@Then("^I press \"([^\"]*)\"$")
public void I_press(String text) throws Throwable {
WebElement item = driver.findElement(By.name(text));
if(item != null && item.isDisplayed()){
item.click();
return;
}
fail("Neither button or text \"" + text + "\" was found");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment