how to test with selenium code example
Example 1: running Seleniam tests
// Sample test in Java to run Automate session.
import org.openqa.selenium.By;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import java.net.URL;
public class JavaSample {
public static final String AUTOMATE_USERNAME = "YOUR_USERNAME";
public static final String AUTOMATE_ACCESS_KEY = "YOUR_ACCESS_KEY";
public static final String URL = "https://" + AUTOMATE_USERNAME + ":" + AUTOMATE_ACCESS_KEY + "@hub-cloud.browserstack.com/wd/hub";
public static void main(String[] args) throws Exception {
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("os_version", "8");
caps.setCapability("resolution", "1920x1080");
caps.setCapability("browser", "Chrome");
caps.setCapability("browser_version", "latest-beta");
caps.setCapability("os", "Windows");
caps.setCapability("name", "BStack-[Java] Sample Test"); // test name
caps.setCapability("build", "BStack Build Number 1"); // CI/CD job or build name
WebDriver driver = new RemoteWebDriver(new URL(URL), caps);
driver.get("https://www.google.com");
WebElement element = driver.findElement(By.name("q"));
element.sendKeys("BrowserStack");
element.submit();
System.out.println(driver.getTitle());
driver.quit();
}
}
Example 2: how to host selenium web automation scripts online
# The same code that Homeless Horse wrote but in Python
from selenium import webdriver # pip install selenium to work
from selenum.webdriver.common.keys import Keys
driver = webdriver.Chrome() # Specifying which browser to use
driver.get("https://www.google.com") # The url of the website to visit
element = driver.find_element_by_name("q") # Find the searchbar element which has the name "q"
element.send_keys("BrowserStack") # Send in the word "BrowserSTack"
element.send_keys(Keys.RETURN) # Send in the Return Key(Enter Key)
print(driver.title) # Print the title of the webpage in the console
driver.quit() # Close the webdriver