Created
April 10, 2018 22:45
-
-
Save sauceaaron/955b4edb44965796c13885779719a15d to your computer and use it in GitHub Desktop.
Parallel Gradle TestNG SauceLabs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| group 'com.saucelabs.examples' | |
| version '1.0-SNAPSHOT' | |
| apply plugin: 'java' | |
| apply plugin: 'idea' | |
| sourceCompatibility = 1.8 | |
| repositories { | |
| mavenCentral() | |
| } | |
| dependencies { | |
| compile 'org.seleniumhq.selenium:selenium-java:3.4.0' | |
| testCompile 'org.testng:testng:6.14.2' | |
| testCompile 'org.assertj:assertj-core:3.9.0' | |
| } | |
| test { | |
| systemProperties System.getProperties() | |
| dependsOn cleanTest | |
| testLogging.showStandardStreams = true | |
| useTestNG() { | |
| parallel 'methods' | |
| threadCount 10 | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import org.openqa.selenium.remote.DesiredCapabilities; | |
| import org.openqa.selenium.remote.RemoteWebDriver; | |
| import org.testng.annotations.AfterMethod; | |
| import org.testng.annotations.BeforeMethod; | |
| import java.lang.reflect.Method; | |
| import java.net.MalformedURLException; | |
| import java.net.URL; | |
| public class SauceTestBase | |
| { | |
| ThreadLocal<RemoteWebDriver> driver = new ThreadLocal<>(); | |
| public DesiredCapabilities getDesiredCapabilities() | |
| { | |
| DesiredCapabilities capabilities = new DesiredCapabilities(); | |
| capabilities.setCapability("platform", "Windows 10"); | |
| capabilities.setCapability("browserName", "Firefox"); | |
| capabilities.setCapability("version", "latest"); | |
| return capabilities; | |
| } | |
| public URL getSauceURL() | |
| { | |
| String SAUCE_USERNAME = System.getenv("SAUCE_USERNAME"); | |
| System.out.println("SAUCE_USERNAME: " + SAUCE_USERNAME); | |
| String SAUCE_ACCESS_KEY = System.getenv("SAUCE_ACCESS_KEY"); | |
| String SAUCE_URL = "https://" + SAUCE_USERNAME + ":" + SAUCE_ACCESS_KEY + "@ondemand.saucelabs.com:443/wd/hub"; | |
| System.out.println("SAUCE_URL: " + SAUCE_URL); | |
| try | |
| { | |
| return new URL(SAUCE_URL); | |
| } | |
| catch (MalformedURLException e) | |
| { | |
| e.printStackTrace(); | |
| throw new RuntimeException(e); | |
| } | |
| } | |
| public String getTestName(Method method) | |
| { | |
| return (this.getClass().getSimpleName() + " " + method.getName()).replace("_", " "); | |
| } | |
| public RemoteWebDriver webdriver() | |
| { | |
| return driver.get(); | |
| } | |
| @BeforeMethod | |
| public void setup(Method method) | |
| { | |
| String testName = (this.getClass().getSimpleName() + " " + method.getName()).replace("_", " "); | |
| URL url = getSauceURL(); | |
| DesiredCapabilities capabilities = getDesiredCapabilities(); | |
| capabilities.setCapability("name", testName); | |
| RemoteWebDriver driver = new RemoteWebDriver(url, capabilities); | |
| this.driver.set(driver); | |
| webdriver().executeScript("sauce:context=" +testName); | |
| } | |
| @AfterMethod | |
| public void teardown() | |
| { | |
| this.driver.get().quit(); | |
| this.driver.remove(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import org.testng.annotations.Test; | |
| import java.lang.reflect.Method; | |
| public class SimpleSauceTests extends SauceTestBase | |
| { | |
| String site = "https://saucelabs-sample-test-frameworks.github.io/training-test-page"; | |
| @Test | |
| public void testA(Method method) | |
| { | |
| webdriver().get(site); | |
| } | |
| @Test | |
| public void testB(Method method) | |
| { | |
| webdriver().get(site); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import org.testng.annotations.Test; | |
| import static org.assertj.core.api.Assertions.assertThat; | |
| public class SimpleTest extends TestBase | |
| { | |
| @Test | |
| public void sanityTest() | |
| { | |
| Boolean fact = true; | |
| printThreadInfo(); | |
| assertThat(fact).isTrue(); | |
| } | |
| @Test | |
| public void insanityTest() | |
| { | |
| Boolean fact = false; | |
| printThreadInfo(); | |
| assertThat(fact).isFalse(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class TestBase | |
| { | |
| public void printThreadInfo() | |
| { | |
| Thread thread = Thread.currentThread(); | |
| StringBuilder s = new StringBuilder(); | |
| log( | |
| "test: " + thread.getStackTrace()[2], | |
| "thread: " + thread.getId() | |
| ); | |
| pause(5); | |
| } | |
| public void log(String... message) | |
| { | |
| StringBuilder s = new StringBuilder(); | |
| s.append("[log] "); | |
| for(String m : message) { | |
| s.append(m); | |
| s.append(" "); | |
| } | |
| System.out.println(s); | |
| } | |
| public void pause(int seconds) | |
| { | |
| try { | |
| Thread.sleep(seconds * 1000); | |
| } | |
| catch (InterruptedException e) | |
| { | |
| e.printStackTrace(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment