Repository navigation
Expand file tree
/
Copy pathtest.java
More file actions
132 lines (109 loc) · 6.09 KB
/
Copy pathtest.java
File metadata and controls
132 lines (109 loc) · 6.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// NOTE: replace USERNAME:ACCESS_KEY@YOUR_SUBDOMAIN and VIDEO_URL with your credentials found in the Gridlastic dashboard
// ALSO SEE https://github.com/Gridlastic/demo1 FOR JAVA TESTNG EXAMPLES WITH PARALLEL TEST EXECUTIONS
// FOR SELENIUM VERSION 3.53+, FIREFOX VERSIONS 55+ REQUIRES THE BINARY LOCATION ON THE GRID NODE TO BE SPECIFIED.
package java_example;
import java.net.URL;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class test {
// VIDEO_URL set to like "https://s3-ap-southeast-2.amazonaws.com/b2729248-ak68-6948-a2y8-80e7479te16a/9ag7b09j-6a38-58w2-bb01-17qw724ce46t/play.html?".
// Find this VIDEO_URL value in your Gridlastic dashboard.
private static final String VIDEO_URL = null;
private RemoteWebDriver driver;
@BeforeMethod(alwaysRun = true)
public void beforeMethod() throws Exception {
// Example test environment. NOTE: Gridlastic auto scaling requires all
// these 3 environment variables in each request.
String platform_name = "win10";
String browser_name = "chrome";
String browser_version = "latest";// you can use "latest" for chrome only or a specific version number. For Firefox and internet explorer use a specific version number.
// optional video recording
String record_video = "True";
DesiredCapabilities capabilities = new DesiredCapabilities();
if (platform_name.equalsIgnoreCase("win7")) {
capabilities.setPlatform(Platform.VISTA);
capabilities.setCapability("platformName", "windows"); //required from selenium version 3.9.1 when testing with firefox or IE. Required when testing with Chrome 77+.
}
if (platform_name.equalsIgnoreCase("win8")) {
capabilities.setPlatform(Platform.WIN8);
capabilities.setCapability("platformName", "windows"); //required from selenium version 3.9.1 when testing with firefox or IE. Required when testing with Chrome 77+.
}
if (platform_name.equalsIgnoreCase("win8_1")) {
capabilities.setPlatform(Platform.WIN8_1);
capabilities.setCapability("platformName", "windows"); //required from selenium version 3.9.1 when testing with firefox or IE. Required when testing with Chrome 77+.
}
if (platform_name.equalsIgnoreCase("win10")) {
capabilities.setPlatform(Platform.WIN10);
capabilities.setCapability("platformName", "windows"); //required from selenium version 3.9.1 when testing with firefox or IE. Required when testing with Chrome 77+.
}
if (platform_name.equalsIgnoreCase("linux")) {
capabilities.setPlatform(Platform.LINUX);
}
capabilities.setBrowserName(browser_name);
capabilities.setVersion(browser_version);
// video record
if (record_video.equalsIgnoreCase("True")) {
capabilities.setCapability("video", "True"); // NOTE: "True" is a case sensitive string, not boolean.
} else {
capabilities.setCapability("video", "False"); // NOTE: "False" is a case sensitive string, not boolean.
}
//Chrome specifics
if (browser_name.equalsIgnoreCase("chrome")){
ChromeOptions options = new ChromeOptions();
options.addArguments("disable-infobars"); // starting from Chrome 57 the info bar displays with "Chrome is being controlled by automated test software.". This flag has been disabled by Google from Chrome version 75.
// On Linux start-maximized does not expand browser window to max screen size. Always set a window size.
if (platform_name.equalsIgnoreCase("linux")) {
options.addArguments(Arrays.asList("--window-position=0,0"));
options.addArguments(Arrays.asList("--window-size=1920,1080"));
} else {
options.addArguments(Arrays.asList("--start-maximized"));
}
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
}
//Firefox version 55+ specifics
if (browser_name.equalsIgnoreCase("firefox")){
FirefoxOptions ffOptions = new FirefoxOptions();
// Required to specify firefox binary location on Gridlastic grid nodes starting from selenium version 3.5.3+, see firefox documentation https://www.gridlastic.com/test-environments.html#firefox_testing
if (platform_name.equalsIgnoreCase("linux")){
ffOptions.setBinary("/home/ubuntu/Downloads/firefox"+browser_version+"/firefox");
} else {
ffOptions.setBinary("C:\\Program Files (x86)\\Mozilla Firefox\\firefox"+browser_version+"\\firefox.exe");
}
capabilities.setCapability("moz:firefoxOptions", ffOptions);
}
//replace USERNAME:ACCESS_KEY@SUBDOMAIN with your credentials found in the Gridlastic dashboard
driver = new RemoteWebDriver(new URL("http://USERNAME:ACCESS_KEY@YOUR_SUBDOMAIN.gridlastic.com:80/wd/hub"),capabilities);
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
// On LINUX/FIREFOX the "driver.manage().window().maximize()" option does not expand browser window to max screen size. Always set a window size.
if (platform_name.equalsIgnoreCase("linux") && browser_name.equalsIgnoreCase("firefox")) {
driver.manage().window().setSize(new Dimension(1920, 1080));
}
if (record_video.equalsIgnoreCase("True")) {
System.out.println("Test Video: " + VIDEO_URL + ((RemoteWebDriver) driver).getSessionId());
}
}
@Test(enabled = true)
public void test_site() throws Exception {
driver.get("https://www.google.com/ncr");
Thread.sleep(10000); //slow down for demo purposes
WebElement element = driver.findElement(By.name("q"));
element.sendKeys("webdriver");
element.submit();
Thread.sleep(5000); //slow down for demo purposes
}
@AfterMethod(alwaysRun = true)
public void tearDown() throws Exception {
driver.quit();
}
}