Skip to content

Instantly share code, notes, and snippets.

View angelovstanton's full-sized avatar
💭
UPDATED STATUS

Anton Angelov angelovstanton

💭
UPDATED STATUS
View GitHub Profile
@Test
public void navigationHistory() {
driver.navigate().to("https://www.codeproject.com/Articles/1078541/Advanced-WebDriver-Tips-and-Tricks-Part");
driver.navigate().to("http://www.codeproject.com/Articles/1017816/Speed-up-Selenium-Tests-through-RAM-Facts-and-Myth");
driver.navigate().back();
Assert.assertEquals("10 Advanced WebDriver Tips and Tricks - Part 1 - CodeProject", driver.getTitle());
driver.navigate().refresh();
@Test
public void movingBetweenTabs() {
driver.navigate().to("https://www.automatetheplanet.com/");
var firstLink = driver.findElement(By.xpath("//*[@id='menu-item-11362']/a"));
var secondLink = driver.findElement(By.xpath("//*[@id='menu-item-6']/a"));
String selectLinkOpenninNewTab = Keys.chord(Keys.CONTROL,Keys.RETURN);
firstLink.sendKeys(selectLinkOpenninNewTab);
secondLink.sendKeys(selectLinkOpenninNewTab);
Set<String> windows = driver.getWindowHandles();
String firstTab = (String)windows.toArray()[1];
@Test
public void handleJavaScripPopUps() {
driver.navigate().to("http://www.w3schools.com/js/tryit.asp?filename=tryjs_confirm");
driver.switchTo().frame("iframeResult");
var button = driver.findElement(By.xpath("/html/body/button"));
button.click();
Alert alert = driver.switchTo().alert();
if (alert.getText().equals("Press a button!")) {
alert.accept();
@Test
public void fileUpload() throws IOException {
driver.navigate().to(
"https://demos.telerik.com/aspnet-ajax/ajaxpanel/application-scenarios/file-upload/defaultcs.aspx");
var element = driver.findElement(By.id("ctl00_ContentPlaceholder1_RadUpload1file0"));
String filePath = Paths.get(getProperty("java.io.tmpdir"), "debugWebDriver.xml").toString();
File destFile = new File(filePath);
destFile.createNewFile();
@Test
public void dragAndDrop() {
driver.navigate().to("http://loopj.com/jquery-simple-slider/");
var element = driver.findElement(By.xpath("//*[@id='project']/p[1]/div/div[2]"));
Actions action = new Actions(driver);
action.dragAndDropBy(element, 30, 0).build().perform();
}
driver.manage().window().maximize();
@Test
public void manageCookies() {
driver.navigate().to("http://automatetheplanet.com");
// get all cookies
var cookies = driver.manage().getCookies();
for (Cookie cookie:cookies) {
System.out.println(cookie.getName());
}
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addExtensions(new File("local/path/to/extension.crx"));
driver = new ChromeDriver(chromeOptions);
ProfilesIni profile = new ProfilesIni();
FirefoxProfile firefoxProfile = profile.getProfile("xyzProfile");
String downloadFilepath = "c:\\temp";
firefoxProfile.setPreference("browser.download.folderList", 2);
firefoxProfile.setPreference("browser.download.dir", downloadFilepath);
firefoxProfile.setPreference("browser.download.manager.alertOnEXEOpen", false);
firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk",
"application/msword, application/binary, application/ris, text/csv, image/png, application/pdf, text/html, text/plain, application/zip, application/x-zip, application/x-zip-compressed, application/download, application/octet-stream"));
FirefoxOptions firefoxOptions = new FirefoxOptions();
@Test
public void checkIfElementIsVisible() {
driver.navigate().to("http://automatetheplanet.com");
var element = driver.findElement(By.xpath("/html/body/div[1]/header/div/div[2]/div/div[2]/nav");
Assert.assertTrue(element).isDisplayed());
}