Created
June 3, 2021 07:39
-
-
Save Jaimin180296/1b24611f3980b4718b7ea824cd6923ba 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
package BrowserStack; | |
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.support.ui.ExpectedConditions; | |
import org.openqa.selenium.support.ui.WebDriverWait; | |
import java.net.MalformedURLException; | |
import java.net.URL; | |
import java.util.concurrent.TimeUnit; | |
public class WedbdriverWait { | |
public static final String AUTOMATE_USERNAME = "<username>"; | |
public static final String AUTOMATE_ACCESS_KEY = "<acesskey>"; | |
public static final String URL = "http://" + AUTOMATE_USERNAME + ":" + AUTOMATE_ACCESS_KEY + "@hub-cloud.browserstack.com/wd/hub"; | |
static WebDriver driver; | |
public static void main(String[] args) throws MalformedURLException { | |
DesiredCapabilities caps = new DesiredCapabilities(); | |
caps.setCapability("os", "OS X"); | |
caps.setCapability("os_version", "Big Sur"); | |
caps.setCapability("browser", "Firefox"); | |
caps.setCapability("browser_version", "88.0"); | |
caps.setCapability("browserstack.ie.arch", ""); | |
caps.setCapability("browserstack.ie.driver", ""); | |
caps.setCapability("browserstack.selenium_version", "3.141.59"); | |
caps.setCapability("browserstack.appium_version", "1.19.1"); | |
caps.setCapability("browserstack.local", "false"); | |
caps.setCapability("build", "ticket#440288"); | |
driver = new RemoteWebDriver(new URL(URL), caps); | |
driver.manage().timeouts().setScriptTimeout(1, TimeUnit.SECONDS); | |
driver.get("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