Skip to content

Commit fef1a6d

Browse files
committed
Initial commit.
0 parents  commit fef1a6d

4 files changed

Lines changed: 411 additions & 0 deletions

File tree

‎selenium-grid-java-example/pom.xml‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>selenium-grid-java-example</groupId>
6+
<artifactId>selenium-grid-java-example</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<dependencies>
9+
<dependency>
10+
<groupId>com.google.guava</groupId>
11+
<artifactId>guava</artifactId>
12+
<version>28.2-jre</version>
13+
</dependency>
14+
<dependency>
15+
<groupId>org.testng</groupId>
16+
<artifactId>testng</artifactId>
17+
<version>7.1.0</version>
18+
<scope>test</scope>
19+
</dependency>
20+
<dependency>
21+
<groupId>org.seleniumhq.selenium</groupId>
22+
<artifactId>selenium-java</artifactId>
23+
<version>3.14.0</version>
24+
</dependency>
25+
</dependencies>
26+
<build>
27+
<plugins>
28+
<plugin>
29+
<artifactId>maven-compiler-plugin</artifactId>
30+
<version>3.8.1</version>
31+
<configuration>
32+
<source>1.8</source>
33+
<target>1.8</target>
34+
</configuration>
35+
</plugin>
36+
<plugin>
37+
<groupId>org.apache.maven.plugins</groupId>
38+
<artifactId>maven-surefire-plugin</artifactId>
39+
<version>3.0.0-M4</version>
40+
<configuration>
41+
<suiteXmlFiles>
42+
<suiteXmlFile>${basedir}/src/test/java/java_example/testng.xml</suiteXmlFile>
43+
</suiteXmlFiles>
44+
<systemPropertyVariables>
45+
<hub>${hub}</hub>
46+
<videoUrl>${videoUrl}</videoUrl>
47+
</systemPropertyVariables>
48+
</configuration>
49+
</plugin>
50+
</plugins>
51+
</build>
52+
</project>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package java_example;
2+
3+
4+
import org.testng.ITestResult;
5+
import org.testng.TestListenerAdapter;
6+
7+
public class CustomListener extends TestListenerAdapter{
8+
private int m_count = 0;
9+
10+
@Override
11+
public void onTestFailure(ITestResult tr) {
12+
System.out.println("******************** FAILURE: " + tr.getTestContext().getCurrentXmlTest().getParameter("test-title") + " " +tr.getTestContext().getCurrentXmlTest().getParameter("platform-name") + " TEST METHOD: "+tr.getMethod().getMethodName() +" REASON:"+tr.getThrowable().getMessage().substring(0, 30).trim()+"... VIDEO: "+tr.getTestContext().getAttribute("video_url"));
13+
14+
}
15+
16+
@Override
17+
public void onTestSkipped(ITestResult tr) {
18+
log(tr.getName()+ "--Test method skipped\n");
19+
}
20+
21+
22+
@Override
23+
public void onTestSuccess(ITestResult tr) {
24+
if(tr.getTestContext().getCurrentXmlTest().getParameter("record-video").equalsIgnoreCase("True")){
25+
System.out.println("SUCCESS: " + tr.getTestContext().getCurrentXmlTest().getParameter("test-title") + " TEST METHOD: "+tr.getMethod().getMethodName() +"... VIDEO: "+tr.getTestContext().getAttribute("video_url"));
26+
} else {
27+
System.out.println("SUCCESS: " + tr.getTestContext().getCurrentXmlTest().getParameter("test-title") + " TEST METHOD: "+tr.getMethod().getMethodName());
28+
}
29+
}
30+
31+
32+
private void log(String string) {
33+
System.out.print(string);
34+
if (++m_count % 40 == 0) {
35+
System.out.println("");
36+
}
37+
}
38+
39+
40+
41+
42+
43+
44+
45+
}
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
// NOTE: RUN THIS EXAMPLE AS A MAVEN PROJECT WITH THE HUB AND VIDEO URL PARAMETERS LIKE
2+
// GOAL: test -Dhub=https://USERNAME:ACCESS_KEY@HUB_SUBDOMAIN.gridlastic.com/wd/hub -DvideoUrl=VIDEO_URL
3+
// USERNAME:ACCESS_KEY@HUB_SUBDOMAIN and VIDEO_URL is found in the Gridlastic dashboard after you start your selenium grid.
4+
5+
6+
package java_example;
7+
8+
import java.net.URL;
9+
import java.util.Arrays;
10+
import java.util.concurrent.TimeUnit;
11+
12+
import org.openqa.selenium.By;
13+
import org.openqa.selenium.Dimension;
14+
import org.openqa.selenium.WebElement;
15+
import org.openqa.selenium.chrome.ChromeOptions;
16+
import org.openqa.selenium.edge.EdgeOptions;
17+
import org.openqa.selenium.remote.RemoteWebDriver;
18+
import org.openqa.selenium.firefox.FirefoxOptions;
19+
import org.openqa.selenium.ie.InternetExplorerOptions;
20+
import org.testng.ITestContext;
21+
import org.testng.annotations.AfterMethod;
22+
import org.testng.annotations.BeforeMethod;
23+
import org.testng.annotations.Parameters;
24+
import org.testng.annotations.Test;
25+
26+
public class test {
27+
28+
private RemoteWebDriver driver;
29+
ITestContext myTestContext;
30+
31+
@Parameters({ "browser-name", "platform-name", "browser-version", "hub","videoUrl", "record-video" })
32+
@BeforeMethod(alwaysRun = true)
33+
public void beforeMethod(String browser_name, String platform_name, String browser_version, String hub, String videoUrl, String record_video,ITestContext myTestContext) throws Exception {
34+
35+
36+
//CHROME specifics
37+
if (browser_name.equalsIgnoreCase("chrome")){
38+
ChromeOptions options = new ChromeOptions();
39+
options.setCapability("version", browser_version);
40+
options.setCapability("platform", platform_name);
41+
if (!platform_name.equalsIgnoreCase("linux")){
42+
options.setCapability("platformName", "windows"); //required from selenium version 3.9.1 when testing with firefox or IE. Required when testing with Chrome 77+.
43+
}
44+
options.setCapability("video", record_video); // NOTE: case sensitive string, not boolean.
45+
46+
47+
// On Linux start-maximized does not expand browser window to max screen size. Always set a window size.
48+
if (platform_name.equalsIgnoreCase("linux")) {
49+
options.addArguments(Arrays.asList("--window-position=0,0"));
50+
options.addArguments(Arrays.asList("--window-size=1920,1080"));
51+
} else {
52+
options.addArguments(Arrays.asList("--start-maximized"));
53+
}
54+
driver = new RemoteWebDriver(new URL(hub),options);
55+
}
56+
57+
58+
59+
//FIREFOX version 55+ specifics
60+
if (browser_name.equalsIgnoreCase("firefox")){
61+
FirefoxOptions options = new FirefoxOptions();
62+
options.setCapability("version", browser_version);
63+
options.setCapability("platform", platform_name);
64+
if (!platform_name.equalsIgnoreCase("linux")){
65+
options.setCapability("platformName", "windows");
66+
}
67+
options.setCapability("video", record_video);
68+
69+
// 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
70+
if (platform_name.equalsIgnoreCase("linux")){
71+
options.setBinary("/home/ubuntu/Downloads/firefox"+browser_version+"/firefox");
72+
} else {
73+
options.setBinary("C:\\Program Files (x86)\\Mozilla Firefox\\firefox"+browser_version+"\\firefox.exe");
74+
}
75+
driver = new RemoteWebDriver(new URL(hub),options);
76+
}
77+
78+
79+
// INTERNER EXPLORER specifics
80+
if (browser_name.equalsIgnoreCase("internet explorer")) {
81+
InternetExplorerOptions options = new InternetExplorerOptions();
82+
options.setCapability("version", browser_version);
83+
options.setCapability("platform", platform_name);
84+
options.setCapability("platformName", "windows"); // required from selenium version 3.9.1 when testing with
85+
// firefox or IE. Required when testing with Chrome 77+.
86+
options.setCapability("video", record_video); // NOTE: case sensitive string, not boolean.
87+
88+
driver = new RemoteWebDriver(new URL(hub), options);
89+
}
90+
91+
92+
// MICROSOFT EDGE specifics
93+
if (browser_name.equalsIgnoreCase("MicrosoftEdge")) {
94+
EdgeOptions options = new EdgeOptions();
95+
options.setCapability("version", browser_version);
96+
options.setCapability("platform", platform_name);
97+
options.setCapability("platformName", "windows"); // required from selenium version 3.9.1 when testing with
98+
// firefox or IE. Required when testing with Chrome 77+.
99+
options.setCapability("video", record_video); // NOTE: case sensitive string, not boolean.
100+
101+
driver = new RemoteWebDriver(new URL(hub), options);
102+
driver.manage().window().maximize();
103+
}
104+
105+
106+
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
107+
108+
// On LINUX/FIREFOX the "driver.manage().window().maximize()" option does not expand browser window to max screen size. Always set a window size.
109+
if (platform_name.equalsIgnoreCase("linux") && browser_name.equalsIgnoreCase("firefox")) {
110+
driver.manage().window().setSize(new Dimension(1920, 1080));
111+
}
112+
113+
// VIDEO URL
114+
if(record_video.equals("True")){
115+
myTestContext.setAttribute("video_url", videoUrl+"/play.html?" + ((RemoteWebDriver) driver).getSessionId());
116+
} else {
117+
myTestContext.removeAttribute("video_url");
118+
}
119+
120+
}
121+
122+
@Parameters({"test-title"})
123+
@Test
124+
public void test_site(String test_title, ITestContext myTestContext) throws Exception {
125+
driver.get("https://www.google.com/ncr");
126+
Thread.sleep(10000); //slow down for demo purposes
127+
WebElement element = driver.findElement(By.name("q"));
128+
element.sendKeys("webdriver");
129+
element.submit();
130+
Thread.sleep(5000); //slow down for demo purposes
131+
}
132+
133+
@AfterMethod(alwaysRun = true)
134+
public void tearDown() throws Exception {
135+
driver.quit();
136+
}
137+
138+
}
139+

0 commit comments

Comments
 (0)