Created
May 13, 2021 02:06
-
-
Save Jaimin180296/51ca3e5164bf5bd9c8faf2dbc1dae778 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import org.junit.Test; | |
| import org.openqa.selenium.By; | |
| import org.openqa.selenium.JavascriptExecutor; | |
| import org.openqa.selenium.WebDriver; | |
| import org.openqa.selenium.WebElement; | |
| import org.openqa.selenium.remote.DesiredCapabilities; | |
| import org.openqa.selenium.remote.RemoteWebDriver; | |
| import org.openqa.selenium.remote.SessionId; | |
| import org.openqa.selenium.support.ui.ExpectedConditions; | |
| import org.openqa.selenium.support.ui.WebDriverWait; | |
| import java.net.MalformedURLException; | |
| import java.net.URL; | |
| public class JunitExample { | |
| public WebDriver driver; | |
| public WebDriverWait wait; | |
| @Test | |
| public void test() throws MalformedURLException { | |
| String username = "jaiminmehta3"; | |
| String accessKey = "f2q1FMatZb3X88b19jQ6"; | |
| final String URL = "https://" + username + ":" + accessKey + "@hub-cloud.browserstack.com/wd/hub"; | |
| System.out.println("Key------->" + username); | |
| System.out.println("Acesskey------->" + accessKey); | |
| DesiredCapabilities caps = new DesiredCapabilities(); | |
| caps.setCapability("os", "Windows"); | |
| caps.setCapability("os_version", "10"); | |
| caps.setCapability("browser", "Chrome"); | |
| caps.setCapability("browser_version", "89.0"); | |
| caps.setCapability("browserstack.local", "false"); | |
| caps.setCapability("browserstack.selenium_version", "3.14.0"); | |
| driver = new RemoteWebDriver(new URL(URL), caps); | |
| SessionId session = ((RemoteWebDriver) driver).getSessionId(); | |
| System.out.println("Session id: " + session.toString()); | |
| driver.navigate().to("https://www.google.com/"); | |
| WebElement element = driver.findElement(By.name("q")); | |
| element.sendKeys("BrowserStack"); | |
| element.submit(); | |
| // Setting the status of test as 'passed' or 'failed' based on the condition; if title of the web page contains 'BrowserStack' | |
| WebDriverWait wait = new WebDriverWait(driver, 5); | |
| try { | |
| wait.until(ExpectedConditions.titleContains("BrowserStack")); | |
| markTestStatus("passed", "Yaay title contains 'BrowserStack'!", driver); | |
| } catch (Exception e) { | |
| markTestStatus("failed", "Naay title does not contain 'BrowserStack'!", driver); | |
| } | |
| System.out.println(driver.getTitle()); | |
| driver.quit(); | |
| } | |
| // This method accepts the status, reason and WebDriver instance and marks the test on BrowserStack | |
| public static void markTestStatus(String status, String reason, WebDriver driver) { | |
| JavascriptExecutor jse = (JavascriptExecutor) driver; | |
| jse.executeScript("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\": \"" + status + "\", \"reason\": \"" + reason + "\"}}"); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment