Skip to content

Instantly share code, notes, and snippets.

@sauceaaron
Created June 7, 2019 22:28
Show Gist options
  • Save sauceaaron/f26a4451c3f3ae02065eba41c7a99809 to your computer and use it in GitHub Desktop.
Save sauceaaron/f26a4451c3f3ae02065eba41c7a99809 to your computer and use it in GitHub Desktop.
Make sure that all engagements are loaded and click on the scope button of the right engagement
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.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
public class EngagementScopeSelectionTest
{
String username = System.getenv("SAUCE_USERNAME");
String accessKey = System.getenv("SAUCE_ACCESS_KEY");
String employeePassword = System.getenv("EMPLOYEE_PASSWORD");
String employeeUsername = System.getenv("EMPLOYEE_USERNAME");
String siteUrl = System.getenv("SITE_URL");
WebDriver driver;
WebDriverWait wait;
@Test
public void selectScope()
{
driver.get(siteUrl);
// should be on Home Realm Discovery
waitForTitle(HomeRealmDiscovery.title);
waitForActiveElement(HomeRealmDiscovery.employeeButton).click();
// should be on login page
waitForTitle(LoginPage.title);
waitForActiveElement(LoginPage.username).sendKeys(employeeUsername);
waitForActiveElement(LoginPage.password).sendKeys(employeePassword);
waitForActiveElement(LoginPage.submitButton).click();
// should be on Engagements page
// waitForTitle(EngagementListPage.title); // this actually just has "Tax Connect" as the title so we can't go by the title
wait.until(ExpectedConditions.urlContains("engagement-list"));
// make sure it is done loading engagements
wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.cssSelector("#home-grid .engagement-client-data #displayName")));
List<WebElement> engagements = driver.findElements(EngagementListPage.engagements);
System.out.println("number of engagements found: " + engagements.size());
// get the engagement called "scope tests"
WebElement scopeTestsEngagement = engagements.stream().filter(element ->
element.getText().contains("scope tests")
).findAny().get();
System.out.println(scopeTestsEngagement.getText());
// alternately find a single engagement by using xpath to search for text
// WebElement scopeTestsEngagement = driver.findElement(By.xpath("//div[@class='engagement-client-data']//*[contains(text(), 'scope tests')]/parent::div"));
// get all the buttons for that particular engagement
List<WebElement> buttons = scopeTestsEngagement.findElements(By.cssSelector("#displayName"));
WebElement scopeButton = buttons.stream().filter(element->
element.getText().contains("Scope")
).findFirst().get();
// alternately find the scope button using xpath to check for text
// WebElement scopeButton = scopeTestsEngagement.findElement(By.xpath("//*[contains(text(), 'Scope')]"));
// alternately find the scope button by link text
// scopeTestsEngagement.findElement(By.linkText("Scope"));
//alternately find it all with one big ugly xpath
// driver.findElement(
// By.xpath("//div[@class='engagement-client-data']//*[contains(text(), 'scope tests')]/parent::div//*[contains(text(), 'Scope')]")
// ).click();
// click the scope button
scopeButton.click();
// wait until on the Scope page
waitForUrl(ScopePage.urlPath);
// get engagement title to make sure we clicked on the right button
String enagementTitleFromScopePage = waitForActiveElement(ScopePage.engagmentTitle).getText();
System.out.println(enagementTitleFromScopePage);
assertThat(enagementTitleFromScopePage).contains("scope tests");
}
@Before
public void setup() throws MalformedURLException
{
URL sauceURL = new URL("https://" + username + ":" + accessKey + "@" + "ondemand.saucelabs.com/wd/hub");
System.out.println(sauceURL);
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("browserName", "chrome");
capabilities.setCapability("version", "latest");
capabilities.setCapability("platform", "MacOS 10.13");
capabilities.setCapability("name", this.getClass().getSimpleName());
driver = new RemoteWebDriver(sauceURL, capabilities);
wait = new WebDriverWait(driver, 30);
}
@After
public void teardown()
{
driver.quit();
}
public void waitForTitle(String title)
{
wait.until(ExpectedConditions.titleContains(title));
}
public void waitForUrl(String path)
{
wait.until(ExpectedConditions.urlContains(path));
}
public WebElement waitForActiveElement(By locator)
{
return wait.until(ExpectedConditions.elementToBeClickable(locator));
}
public List<WebElement> waitForAllElements(By locator)
{
return wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(locator));
}
public static class HomeRealmDiscovery
{
public static String title = "Home Realm Discovery";
public static By employeeButton = By.cssSelector("img[alt='Active Directory']");
}
public static class LoginPage
{
public static String title = "Sign In";
public static By username = By.id("userNameInput");
public static By password = By.id("passwordInput");
public static By submitButton = By.id("submitButton");
}
public static class EngagementListPage
{
public static String title = "Engagement List - Tax Connect";
public static String urlPath = "/engagement-list";
public static By loader = By.className("loader");
public static By engagements = By.cssSelector("#home-grid .engagement-client-data");
public static By buttons = By.id("displayName");
}
public static class ScopePage
{
public static String title = "Scope - Tax Year ...";
public static String urlPath = "/scope/";
public static By engagmentTitle = By.id("engagement-name");
public static By engagementClientName = By.id("engagement-client-name");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment