|
| 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