Skip to content

Instantly share code, notes, and snippets.

View ultimate-qa2's full-sized avatar

Ultimate QA ultimate-qa2

View GitHub Profile
// Good Class Names
public class InsuranceCost
//Bad Class Names
public class Insurancecost
public class insurancecost
public class Insurance_cost
@Test
public void cIsForCookie() {
driver.get("http://example.com");
Cookie cookie = new Cookie.Builder("name", "value")
.domain("example.com")
.expiresOn(new Date(2025,10,31))
.isHttpOnly(true)
.isSecure(false)
.path("/myPath")
@Test
public void alerts() {
driver.get("https://the-internet.herokuapp.com/javascript_alerts");
driver.findElement(By.xPath("//*[contains(normalize-space(text()), 'Click for JS Alert')]")).click();
driver.switchTo().alert().accept();
driver.findElement(By.xPath("//*[contains(normalize-space(text()), 'Click for JS Prompt')]")).click();
Alert inputAlert = driver.switchTo().alert();
String text = inputAlert.getText();
@Test
public void frames() {
driver.navigate().to("https://the-internet.herokuapp.com/nested_frames");
WebElement defaultFrame = driver.findElement(By.name("frame-top"));
driver.switchTo().frame(1);
Assert.assertEquals("BOTTOM", driver.findElement(By.tagName("body")).getText());
driver.switchTo().parentFrame();
driver.switchTo().frame("frame-top");
driver.switchTo().frame("frame-left");
@Test
public void windowFrames() {
driver.navigate().to("https://the-internet.herokuapp.com/windows");
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript(("window.open('https://the-internet.herokuapp.com/windows/new')"));
String originalWindow = driver.getWindowHandle();
Set handles = driver.getWindowHandles();
handles.remove(originalWindow);
@ultimate-qa2
ultimate-qa2 / JavaScriptExecutorExample.cs
Created February 27, 2021 09:05
How to use JavaScript executor in Java Selenium
JavaScriptExecutor js = (JavaScriptExecutor) driver;
js.executeScript("alert('Hello World!')");
@ultimate-qa2
ultimate-qa2 / AdvancedActionsSeleniumJava.cs
Created February 26, 2021 15:47
Advanced mouse and keyboard actions examples in Selenium WebDriver Java
// hover
action.moveToElement(element).build().perform();
// drag and drop
action.dragAndDrop(element, element2).build().perform();
// click-pause-release
action.clickAndHold(element).pause(100).release().build().perform);
@ultimate-qa2
ultimate-qa2 / KeyboardActions.cs
Created February 26, 2021 15:43
Keyboard actions in Java
// Performs a modified key press.
keyDown(java.lang.CharSequence key);
// Performs a modified key release.
keyUp(java.lang.CharSequence key);
// Sends keys to the active element.
sendKeys(java.lang.CharSequence key);
@ultimate-qa2
ultimate-qa2 / MouseActions.cs
Created February 26, 2021 15:40
Mouse actions in Java
// Clicks in the middle of the given element.
click(WebElement target);
// Clicks (without releasing) at the current mouse location.
clickAndHold(WebElement target);
// Performs a context-click at middle of the given element. First performs a mouseMove to the location of the element.
contextClick(WebElement target);
// Performs a double-click at middle of the given element.
doubleClick(WebElement target);
// A convenience method that performs click-and-hold at the location of the source element,
// moves to the location of the target element, then releases the mouse.
@ultimate-qa2
ultimate-qa2 / KeyboardActionsExample.cs
Last active February 26, 2021 15:33
Performing advanced keyboard actions with Selenium WebDriver in Java
WebElement element = driver.findElement(By.id("sign-in"));
Actions action = new Actions(driver);
action.click(Element).build().perform();