Page History
...
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
package com.yourcompany.Tests; import com.saucelabs.common.SauceOnDemandAuthentication; import com.saucelabs.common.SauceOnDemandSessionIdProvider; import com.saucelabs.testng.SauceOnDemandAuthenticationProvider; import com.saucelabs.testng.SauceOnDemandTestListener; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.remote.CapabilityType; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.remote.RemoteWebDriver; import org.testng.ITestResult; import org.testng.annotations.AfterMethod; import org.testng.annotations.DataProvider; import org.testng.annotations.Listeners; import java.lang.reflect.Method; import java.net.MalformedURLException; import java.net.URL; import java.rmi.UnexpectedException; /** * Simple TestNG test which demonstrates being instantiated via a DataProvider in order to supply multiple browser combinations. * * @author Neil Manvar */ public class TestBase { public String buildTag = System.getenv("BUILD_TAG"); public String username = System.getenv("SAUCE_USERNAME"); public String accesskey = System.getenv("SAUCE_ACCESS_KEY"); /** * ThreadLocal variable which contains the {@link WebDriver} instance which is used to perform browser interactions with. */ private ThreadLocal<WebDriver> webDriver = new ThreadLocal<WebDriver>(); /** * ThreadLocal variable which contains the Sauce Job Id. */ private ThreadLocal<String> sessionId = new ThreadLocal<String>(); /** * DataProvider that explicitly sets the browser combinations to be used. * * @param testMethod * @return Two dimensional array of objects with browser, version, and platform information */ @DataProvider(name = "hardCodedBrowsers", parallel = true) public static Object[][] sauceBrowserDataProvider(Method testMethod) { return new Object[][]{ new Object[]{"MicrosoftEdge", "14.14393", "Windows 10"}, new Object[]{"firefox", "49.0", "Windows 10"}, new Object[]{"internet explorer", "11.0", "Windows 7"}, new Object[]{"safari", "10.0", "OS X 10.11"}, new Object[]{"chrome", "54.0", "OS X 10.10"}, new Object[]{"firefox", "latest-1", "Windows 7"}, }; } /** * @return the {@link WebDriver} for the current thread */ public WebDriver getWebDriver() { return webDriver.get(); } /** * * @return the Sauce Job id for the current thread */ public String getSessionId() { return sessionId.get(); } /** * Constructs a new {@link RemoteWebDriver} instance which is configured to use the capabilities defined by the browser, * version and os parameters, and which is configured to run against ondemand.saucelabs.com, using * the username and access key populated by the {@link #authentication} instance. * * @param browser Represents the browser to be used as part of the test run. * @param version Represents the version of the browser to be used as part of the test run. * @param os Represents the operating system to be used as part of the test run. * @param methodName Represents the name of the test case that will be used to identify the test on Sauce. * @return * @throws MalformedURLException if an error occurs parsing the url */ protected void createDriver(String browser, String version, String os, String methodName) throws MalformedURLException, UnexpectedException { DesiredCapabilities capabilities = new DesiredCapabilities(); // set desired capabilities to launch appropriate browser on Sauce capabilities.setCapability(CapabilityType.BROWSER_NAME, browser); capabilities.setCapability(CapabilityType.VERSION, version); capabilities.setCapability(CapabilityType.PLATFORM, os); capabilities.setCapability("name", methodName); if (buildTag != null) { capabilities.setCapability("build", buildTag); } // Launch remote browser and set it as the current thread webDriver.set(new RemoteWebDriver( new URL("https://" + username + ":" + accesskey + "@ondemand.saucelabs.com:443/wd/hub"), capabilities)); // set current sessionId String id = ((RemoteWebDriver) getWebDriver()).getSessionId().toString(); sessionId.set(id); } /** * Method that gets invoked after test. * Dumps browser log and * Closes the browser */ @AfterMethod public void tearDown(ITestResult result) throws Exception { ((JavascriptExecutor) webDriver.get()).executeScript("sauce:job-result=" + (result.isSuccess() ? "passed" : "failed")); webDriver.get().quit(); } protected void annotate(String text) { ((JavascriptExecutor) webDriver.get()).executeScript("sauce:context=" + text); } } |
The The tests use the Page Objects pattern, implemented by the following class.
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
package com.yourcompany.Pages; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.FindBy; import org.openqa.selenium.support.PageFactory; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; public class GuineaPigPage { @FindBy(linkText = "i am a link") private WebElement theActiveLink; @FindBy(id = "your_comments") private WebElement yourCommentsSpan; @FindBy(id = "comments") private WebElement commentsTextAreaInput; @FindBy(id = "submit") private WebElement submitButton; public WebDriver driver; public static String url = "https://saucelabs-sample-test-frameworks.github.io/training-test-page"; public static GuineaPigPage visitPage(WebDriver driver) { GuineaPigPage page = new GuineaPigPage(driver); page.visitPage(); return page; } public GuineaPigPage(WebDriver driver) { this.driver = driver; PageFactory.initElements(driver, this); } public void visitPage() { this.driver.get(url); } public void followLink() { theActiveLink.click(); } public void submitComment(String text) { commentsTextAreaInput.sendKeys(text); submitButton.click(); // Race condition for time to populate yourCommentsSpan WebDriverWait wait = new WebDriverWait(driver, 15); wait.until(ExpectedConditions.textToBePresentInElement(yourCommentsSpan, text)); } public String getSubmittedCommentText() { return yourCommentsSpan.getText(); } public boolean isOnPage() { String title = "I am a page title - Sauce Labs"; return driver.getTitle() == title; } } |
...