Skip to content

Instantly share code, notes, and snippets.

@bbrother92
Last active June 28, 2017 16:37
Show Gist options
  • Save bbrother92/bd073d26b624619b16e0df61259bf69b to your computer and use it in GitHub Desktop.
Save bbrother92/bd073d26b624619b16e0df61259bf69b to your computer and use it in GitHub Desktop.
Selenium cheatsheet #java

Selenium cheatsheet :octocat: 👍

Start

Chrome:

File file = new File("D:/Selenium/chromedriver.exe");
System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); // optional

Firefox:

File pathBinary = new File("C:\\Program Files (x86)\\firefox\\firefox.exe");
FirefoxBinary firefoxBinary = new FirefoxBinary(pathBinary);
FirefoxProfile firefoxProfile = new FirefoxProfile(firefoxBinary,firefoxProfile);

OR

System.setProperty("webdriver.firefox.bin", "C:\\Program Files (x86)\\firefox\\firefox.exe");

Opening new windows

/**
 * via actions
 */
Actions newTab=new Actions(driver);
newTab.keyDown(Keys.SHIFT).click(driver.findElement(By.xpath("//input[@id='gh-btn']"))).keyUp(Keys.SHIFT).build().perform();
/**
 * via js
 */
((JavascriptExecutor)driver).executeScript("window.open();");
System.out.println(driver.getWindowHandles());
driver.switchTo().window((driver.getWindowHandles().toArray())[1].toString());

Cookie

Cookie ck = new Cookie(name,value,domain,path,date);
System.out.println(ck);
driver.manage().addCookie(ck);

JS executor

Click on element by id

JavascriptExecutor jse = (JavascriptExecutor) driver; jse.executeScript("document.getElementById('gh-eb-Geo-a-en').click();");

Hover mouse on item menu and click

Actions actions = new Actions(driver);
actions.moveToElement(language).perform();
WebElement element = driver.findElement(By.id("gh-eb-Geo-a-en"));
actions.moveToElement(element2);
actions.click().build().perform();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment