Skip to content

Instantly share code, notes, and snippets.

@muditlambda
Created September 30, 2020 14:13
package org.selenium4;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import java.net.MalformedURLException;
import java.net.URL;
import static org.junit.Assert.*;
public class CrossBrowserTest {
WebDriver driver = null;
String URL = "https://manytools.org/http-html-text/browser-language/";
public static String status = "passed";
String username = "user-name";
String access_key = "access-key";
@BeforeClass
public void testSetUp() throws MalformedURLException {
FirefoxOptions firefoxOptions = new FirefoxOptions();
FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.setPreference("intl.accept_languages", "ja-JP");
firefoxOptions.setProfile(firefoxProfile);
firefoxOptions.setCapability("build", "[Java] Locale Testing with Firefox & macOS on LambdaTest Selenium Grid");
firefoxOptions.setCapability("name", "[Java] Locale Testing with Firefox & macOS on LambdaTest Selenium Grid");
firefoxOptions.setCapability("platform", "macOS Mojave");
firefoxOptions.setCapability("browserName", "Firefox");
firefoxOptions.setCapability("version","78.0");
firefoxOptions.setCapability("tunnel",false);
firefoxOptions.setCapability("network",true);
firefoxOptions.setCapability("console",true);
firefoxOptions.setCapability("visual",true);
driver = new RemoteWebDriver(new URL("http://" + username + ":" + access_key + "@hub.lambdatest.com/wd/hub"), firefoxOptions);
System.out.println("Started session");
}
@Test
public void test_Selenium4_ToDoApp() throws InterruptedException {
driver.navigate().to(URL);
driver.manage().window().maximize();
JavascriptExecutor executor = (JavascriptExecutor) driver;
String language = executor.executeScript("return window.navigator.userlanguage || window.navigator.language").toString();
/* driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); */
/* Not a good programming practice, added for demonstration */
Thread.sleep(5000);
assertEquals(language, "ja-JP");
Thread.sleep(2000);
}
@AfterClass
public void tearDown() {
if (driver != null) {
((JavascriptExecutor) driver).executeScript("lambda-status=" + status);
driver.quit();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment