Skip to content

Support for running tests on BrowserStack #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ Running test
Go to your project directory from terminal and hit following commands
* `mvn test (defualt will run on local firefox browser)`
* `mvn test "-Dbrowser=chrome" (to use any other browser)`
* `mvn test "-Dcloud_config=saucelab_windows_chrome52" (to run test on cloud test platforms)`
* `mvn test "-Dcloud_config=saucelab_windows_chrome52" (to run test on SauceLabs cloud platform)`
* `mvn test "-Dcloud_config=browserstack_windows_chrome67" (to run test on BrowserStack cloud platform)`

Using canned tests in your project
----------------------------------
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name=Win10-Chrome67
os=Windows
os_version=10
browserName=Chrome
browserVersion=67
4 changes: 4 additions & 0 deletions src/main/java/cloudPlatformConfigs/browserstack.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
username=your_username
access_key=your_access_key
[email protected]/wd/hub
protocol=http
133 changes: 93 additions & 40 deletions src/main/java/env/Env.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,28 @@
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.safari.SafariDriver;

public class Env
public class Env
{
static WebDriver driver = null;
static String browserName = null;
static String cloudBrowserConfigFile = null;
static String cloudPlatformConfigFile = null;
static String currentPath = System.getProperty("user.dir");
static Properties prop = new Properties();

public static String getBrowserName()
{
browserName = System.getProperty("browser");
cloudBrowserConfigFile = System.getProperty("cloud_config");

if(cloudBrowserConfigFile != null)
{
System.out.println("reading config file");
try {
browserName = cloudBrowserConfigFile.split("_")[0];
InputStream input = new FileInputStream(currentPath+"/src/main/java/cloudBrowserConfigs/"+cloudBrowserConfigFile+".properties");
input.close();

}catch (Exception e) {
e.printStackTrace();
System.exit(0);
Expand All @@ -47,10 +47,10 @@ public static String getBrowserName()
browserName = "ff";
return browserName;
}

public static WebDriver SaucelabDriver(){
System.out.println("Creating Saucelab Driver");

try {
InputStream input = new FileInputStream(currentPath+"/src/main/java/cloudPlatformConfigs/saucelab.properties");
prop.load(input);
Expand All @@ -63,19 +63,19 @@ public static WebDriver SaucelabDriver(){
}
input.close();
prop.clear();

String url = saucelabConfig.get("protocol")+
"://"+
saucelabConfig.get("username")+
":"+
saucelabConfig.get("access_key")+
saucelabConfig.get("url");
System.out.println("url :"+url);
URL remoteDriverURL = new URL(url);
DesiredCapabilities capability = new DesiredCapabilities();
input = new FileInputStream(currentPath+"/src/main/java/cloudBrowserConfigs/"+cloudBrowserConfigFile+".properties");
"://"+
saucelabConfig.get("username")+
":"+
saucelabConfig.get("access_key")+
saucelabConfig.get("url");

System.out.println("url :"+url);
URL remoteDriverURL = new URL(url);

DesiredCapabilities capability = new DesiredCapabilities();
input = new FileInputStream(currentPath+"/src/main/java/cloudBrowserConfigs/"+cloudBrowserConfigFile+".properties");
prop.load(input);
enuKeys = prop.keys();
while (enuKeys.hasMoreElements()) {
Expand All @@ -85,22 +85,70 @@ public static WebDriver SaucelabDriver(){
capability.setCapability(key, value);
}
input.close();
driver = new RemoteWebDriver(remoteDriverURL, capability);

driver = new RemoteWebDriver(remoteDriverURL, capability);

} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}

return driver;
}


public static WebDriver BrowserStackDriver(){
System.out.println("Creating BrowserStack Driver");

try {
InputStream input = new FileInputStream(currentPath+"/src/main/java/cloudPlatformConfigs/browserstack.properties");
prop.load(input);
HashMap<String, String> browserstackConfig = new HashMap<String, String>();
Enumeration enuKeys = prop.keys();
while (enuKeys.hasMoreElements()) {
String key = (String) enuKeys.nextElement();
String value = prop.getProperty(key);
browserstackConfig.put(key, value);
}
input.close();
prop.clear();

String url = browserstackConfig.get("protocol")+
"://"+
browserstackConfig.get("username")+
":"+
browserstackConfig.get("access_key")+
browserstackConfig.get("url");

System.out.println("url :"+url);
URL remoteDriverURL = new URL(url);

DesiredCapabilities capability = new DesiredCapabilities();
input = new FileInputStream(currentPath+"/src/main/java/cloudBrowserConfigs/"+cloudBrowserConfigFile+".properties");
prop.load(input);
enuKeys = prop.keys();
while (enuKeys.hasMoreElements()) {
String key = (String) enuKeys.nextElement();
String value = prop.getProperty(key);
System.out.println("key :"+key + " Value :"+value);
capability.setCapability(key, value);
}
input.close();

driver = new RemoteWebDriver(remoteDriverURL, capability);

} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}

return driver;
}

public static WebDriver CreateWebDriver(String browser)
{
System.out.println("Browser: " + browser);
System.out.println("Browser: " + browser);

switch (browser.toLowerCase()) {
switch (browser.toLowerCase()) {
case "ff":
case "firefox":
//ProfilesIni allProfiles = new ProfilesIni();
Expand All @@ -122,22 +170,27 @@ public static WebDriver CreateWebDriver(String browser)
case "safari":
driver = new SafariDriver();
break;

case "saucelab":
driver = SaucelabDriver();
break;
default:
System.out.println("Invalid browser name "+browser);
System.exit(0);
break;
}//switch

driver.manage().deleteAllCookies();
driver.manage().window().maximize();
driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
driver.manage().timeouts().setScriptTimeout(60, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

return driver;
}

case "browserstack":
driver = BrowserStackDriver();
break;

default:
System.out.println("Invalid browser name "+browser);
System.exit(0);
break;
}//switch

driver.manage().deleteAllCookies();
driver.manage().window().maximize();
driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
driver.manage().timeouts().setScriptTimeout(60, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

return driver;
}
}