|
| 1 | +package com.browserstack; |
| 2 | + |
| 3 | +import java.io.FileReader; |
| 4 | +import java.net.URL; |
| 5 | +import java.util.HashMap; |
| 6 | +import java.util.Iterator; |
| 7 | +import java.util.Map; |
| 8 | + |
| 9 | +import com.browserstack.local.Local; |
| 10 | + |
| 11 | +import org.json.simple.JSONObject; |
| 12 | +import org.json.simple.parser.JSONParser; |
| 13 | +import org.openqa.selenium.WebDriver; |
| 14 | +import org.openqa.selenium.MutableCapabilities; |
| 15 | +import org.openqa.selenium.remote.RemoteWebDriver; |
| 16 | +import org.testng.annotations.*; |
| 17 | + |
| 18 | +public class BrowserStackTestNGTest { |
| 19 | + public WebDriver driver; |
| 20 | + private static Local l; |
| 21 | + private static JSONObject config; |
| 22 | + private static Map<String, Object> commonCapabilities; |
| 23 | + private static String username; |
| 24 | + private static String accessKey; |
| 25 | + |
| 26 | + @BeforeSuite(alwaysRun=true) |
| 27 | + @Parameters(value = { "config" }) |
| 28 | + public void beforeSuite(String config_file) throws Exception { |
| 29 | + JSONParser parser = new JSONParser(); |
| 30 | + config = (JSONObject) parser.parse(new FileReader("src/test/resources/conf/" + config_file)); |
| 31 | + commonCapabilities = (Map<String, Object>) config.get("capabilities"); |
| 32 | + HashMap bstackOptionsMap = (HashMap) commonCapabilities.get("bstack:options"); |
| 33 | + |
| 34 | + |
| 35 | + username = System.getenv("BROWSERSTACK_USERNAME"); |
| 36 | + if (username == null) { |
| 37 | + username = (String) config.get("user"); |
| 38 | + } |
| 39 | + |
| 40 | + accessKey = System.getenv("BROWSERSTACK_ACCESS_KEY"); |
| 41 | + if (accessKey == null) { |
| 42 | + accessKey = (String) config.get("key"); |
| 43 | + } |
| 44 | + try { |
| 45 | + if ((bstackOptionsMap.get("local") != null && |
| 46 | + bstackOptionsMap.get("local").toString().equalsIgnoreCase("true") && (l == null))) { |
| 47 | + l = new Local(); |
| 48 | + Map<String, String> options = new HashMap<String, String>(); |
| 49 | + options.put("key", accessKey); |
| 50 | + l.start(options); |
| 51 | + } |
| 52 | + } catch (Exception e) { |
| 53 | + System.out.println("Error while start local - " + e); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + @BeforeMethod(alwaysRun = true) |
| 58 | + @org.testng.annotations.Parameters(value = { "config", "environment" }) |
| 59 | + @SuppressWarnings("unchecked") |
| 60 | + public void setUp(String config_file, String environment) throws Exception { |
| 61 | + JSONParser parser = new JSONParser(); |
| 62 | + config = (JSONObject) parser.parse(new FileReader("src/test/resources/conf/" + config_file)); |
| 63 | + JSONObject envs = (JSONObject) config.get("environments"); |
| 64 | + |
| 65 | + MutableCapabilities capabilities = new MutableCapabilities(); |
| 66 | + |
| 67 | + Map<String, Object> envCapabilities = (Map<String, Object>) envs.get(environment); |
| 68 | + Iterator it = envCapabilities.entrySet().iterator(); |
| 69 | + while (it.hasNext()) { |
| 70 | + Map.Entry pair = (Map.Entry) it.next(); |
| 71 | + capabilities.setCapability(pair.getKey().toString(), pair.getValue()); |
| 72 | + } |
| 73 | + |
| 74 | + commonCapabilities = (Map<String, Object>) config.get("capabilities"); |
| 75 | + it = commonCapabilities.entrySet().iterator(); |
| 76 | + while (it.hasNext()) { |
| 77 | + Map.Entry pair = (Map.Entry) it.next(); |
| 78 | + if (capabilities.getCapability(pair.getKey().toString()) == null) { |
| 79 | + capabilities.setCapability(pair.getKey().toString(), pair.getValue()); |
| 80 | + } else if (pair.getKey().toString().equalsIgnoreCase("bstack:options")) { |
| 81 | + HashMap bstackOptionsMap = (HashMap) pair.getValue(); |
| 82 | + bstackOptionsMap.putAll((HashMap) capabilities.getCapability("bstack:options")); |
| 83 | + capabilities.setCapability(pair.getKey().toString(), bstackOptionsMap); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + if (capabilities.getCapability("bstack:options") != null) { |
| 88 | + HashMap bstackOptionsMap = (HashMap) capabilities.getCapability("bstack:options"); |
| 89 | + if ((bstackOptionsMap.get("local") != null && |
| 90 | + bstackOptionsMap.get("local").toString().equalsIgnoreCase("true") && (l == null || !l.isRunning()))) { |
| 91 | + l = new Local(); |
| 92 | + Map<String, String> options = new HashMap<String, String>(); |
| 93 | + options.put("key", accessKey); |
| 94 | + l.start(options); |
| 95 | + } |
| 96 | + bstackOptionsMap.put("source", "testng:sample-master:v1.0"); |
| 97 | + } |
| 98 | + |
| 99 | + driver = new RemoteWebDriver( |
| 100 | + new URL("http://" + username + ":" + accessKey + "@" + config.get("server") + "/wd/hub"), capabilities); |
| 101 | + } |
| 102 | + |
| 103 | + @AfterMethod(alwaysRun = true) |
| 104 | + public void tearDown() { |
| 105 | + driver.quit(); |
| 106 | + } |
| 107 | + |
| 108 | + @AfterSuite(alwaysRun = true) |
| 109 | + public void afterSuite() throws Exception { |
| 110 | + if (l != null) l.stop(); |
| 111 | + } |
| 112 | +} |
0 commit comments