Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ Master branch contains **Selenium 4** samples, for **Selenium 3 - JSON Wire Prot

### Running your tests

- To run a single test, run `mvn test -P single`
- To run tests, run `mvn test -P parallel`
Comment thread
kamal-kaur04 marked this conversation as resolved.
- To run local tests, run `mvn test -P local`
- To run parallel tests, run `mvn test -P parallel`
- To run the test suite, run `mvn test -P suite`

Understand how many parallel sessions you need by using our [Parallel Test Calculator](https://www.browserstack.com/automate/parallel-calculator?ref=github)
Expand All @@ -34,9 +33,8 @@ Master branch contains **Selenium 4** samples, for **Selenium 3 - JSON Wire Prot

### Running your tests

- To run a single test, run `gradle singleTest`
- To run tests, run `gradle parallelTest`
Comment thread
kamal-kaur04 marked this conversation as resolved.
- To run local tests, run `gradle localTest`
- To run parallel tests, run `gradle parallelTest`
- To run the test suite, run `gradle suiteTest`

Understand how many parallel sessions you need by using our [Parallel Test Calculator](https://www.browserstack.com/automate/parallel-calculator?ref=github)
Expand Down
4 changes: 1 addition & 3 deletions config/parallel.testng.xml
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Parallel" thread-count="3" parallel="tests">
<parameter name="config" value="parallel.conf.json"/>
<test name="SingleTestEnv1">
<parameter name="config" value="parallel.conf.json"/>
<parameter name="environment" value="env1"/>
<classes>
<class name="com.browserstack.SingleTest"/>
</classes>
</test>

<test name="SingleTestEnv2">
<parameter name="config" value="parallel.conf.json"/>
<parameter name="environment" value="env2"/>
<classes>
<class name="com.browserstack.SingleTest"/>
</classes>
</test>

<test name="SingleTestEnv3">
<parameter name="config" value="parallel.conf.json"/>
<parameter name="environment" value="env3"/>
<classes>
<class name="com.browserstack.SingleTest"/>
Expand Down
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>config/parallel.testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
Expand Down
68 changes: 42 additions & 26 deletions src/test/java/com/browserstack/BrowserStackTestNGTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,55 @@
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.*;

public class BrowserStackTestNGTest {
public WebDriver driver;
private Local l;
private static JSONObject config;
private static Map<String, Object> commonCapabilities;
private static String username;
private static String accessKey;

@BeforeSuite(alwaysRun=true)
@Parameters(value = { "config" })
public void beforeSuite(String config_file) throws Exception {
JSONParser parser = new JSONParser();
config = (JSONObject) parser.parse(new FileReader("src/test/resources/conf/" + config_file));
commonCapabilities = (Map<String, Object>) config.get("capabilities");

username = System.getenv("BROWSERSTACK_USERNAME");
if (username == null) {
username = (String) config.get("user");
}

accessKey = System.getenv("BROWSERSTACK_ACCESS_KEY");
if (accessKey == null) {
accessKey = (String) config.get("key");
}
try {
if ((commonCapabilities.get("browserstack.local") != null &&
commonCapabilities.get("browserstack.local").toString().equalsIgnoreCase("true") && (l == null))) {
l = new Local();
Map<String, String> options = new HashMap<String, String>();
options.put("key", accessKey);
l.start(options);
}
} catch (Exception e) {
System.out.println("Error while start local - " + e);
}
}

@BeforeMethod(alwaysRun = true)
@org.testng.annotations.Parameters(value = { "config", "environment" })
@SuppressWarnings("unchecked")
public void setUp(String config_file, String environment) throws Exception {
JSONParser parser = new JSONParser();
JSONObject config = (JSONObject) parser.parse(new FileReader("src/test/resources/conf/" + config_file));
config = (JSONObject) parser.parse(new FileReader("src/test/resources/conf/" + config_file));
JSONObject envs = (JSONObject) config.get("environments");

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("browserstack.source", "testng:sample-selenium-3:v1.0");

capabilities.setCapability("browserstack.source", "testng:sample-selenium-3:v1.1");

Map<String, String> envCapabilities = (Map<String, String>) envs.get(environment);
Iterator it = envCapabilities.entrySet().iterator();
Expand All @@ -47,33 +79,17 @@ public void setUp(String config_file, String environment) throws Exception {
}
}

String username = System.getenv("BROWSERSTACK_USERNAME");
if (username == null) {
username = (String) config.get("user");
}

String accessKey = System.getenv("BROWSERSTACK_ACCESS_KEY");
if (accessKey == null) {
accessKey = (String) config.get("key");
}

if (capabilities.getCapability("browserstack.local") != null
&& capabilities.getCapability("browserstack.local") == "true") {
l = new Local();
Map<String, String> options = new HashMap<String, String>();
options.put("key", accessKey);
l.start(options);
}

driver = new RemoteWebDriver(
new URL("https://" + username + ":" + accessKey + "@" + config.get("server") + "/wd/hub"), capabilities);
}

@AfterMethod(alwaysRun = true)
public void tearDown() throws Exception {
driver.quit();
if (l != null) {
l.stop();
}
}

@AfterSuite(alwaysRun = true)
public void afterSuite() throws Exception {
if (l != null) l.stop();
}
}