Topics covered so far:
Commit for the above topics: https://github.com/writetoprabha/SeleniumJavaWorkouts/commit/e3fda49d5f1af777a1c3ba9925835c10c55362b2
driver.manage.timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage.timeouts().pageLoadTimeout(20, TimeUnit.SECONDS); //=> to make selenium wait for 20 seconds for page load
driver.manage.timeouts().setScriptTimeout(20, Timeunit.SECONDS); //=> to make selenium wait for ajax scripts to execute
WebDriverWait wait = new WebDriverWait(driver, 10); => takes the driver and timeout in seconds as input
wait.until(ExpectedConditions.visibilityOf(driver.findElementBy.Id("xyz")));
Set handles = driver.getWindowHandles();
System.out.println(handles);
for(String handle:handles) {
driver.switchTo.window(handle);
driver.get("https://www.google.com");
}
driver.findElement(By.xpath("//button[contains(text(), 'Click for JS Alert')]")).click();
//Waits until the alert is displayed. Usually alerts are immediate.
// But it is always safe to use the explicit wait to wait until the alert is displayed.
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.alertIsPresent());
/*
After the alert is displayed, we need to make the driver point to the alert window.
driver.switchTo() statement is used
After performing actions on the alert, we need to point driver back to browser window. defaultContent() method is used for the same.
*/
Alert alert = driver.switchTo().alert();
alert.accept();
driver.switchTo().defaultContent();
//retrieves the frame count in the web page
int frameCount = driver.findElements(By.tagName("iframe")).size();
if(frameCount > 0) {
driver.switchTo().frame(0);
System.out.println(driver.findElement(By.id("tinymce")).getText());
driver.switchTo().defaultContent();
}
driver.get("https://the-internet.herokuapp.com/drag_and_drop");
WebElement src = driver.findElement(By.id("column-a"));
WebElement dest = driver.findElement(By.id("column-b"));
Actions action = new Actions(driver);
Thread.sleep(3000);
action.dragAndDrop(src, dest).build().perform();
driver.get("https://secure01b.chase.com/web/auth/dashboard");
Set cookiesSet = driver.manage().getCookies(); //returns cookies present in the driver
Iterator cookies = cookiesSet.iterator(); //create the iterator object on the set object
while(cookies.hasNext()) {
Cookie cookie = (Cookie) cookies.next();
System.out.println(cookie.getName() + " = " + cookie.getValue());
}
int x = driver.findElement(By.xpath("//a[contains(text(), 'A/B Testing')]")).getLocation().x;
int y = driver.findElement(By.xpath("//a[contains(text(), 'A/B Testing')]")).getLocation().y;
Commit: https://github.com/writetoprabha/SeleniumJavaWorkouts/commit/164719c218181d95b1c10a8e49dcaf7467052579
Statement:
Class.forName("com.mysql.cj.jdbc.Driver");
Statement:
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/seleniumpractice", "root", "Colb@121");
Statement smt = con.createStatement(); //Returns statement to execute query ResultSet rs = smt.executeQuery("select FIRSTNAME from employeeInfo");
while(rs.next()) {
System.out.println(rs.getString("FIRSTNAME"));
}
Class.forName("com.mysql.cj.jdbc.Driver"); //loads the driver for our next statement
//pass the connection string for MySql database. Returns connection object to create the statement
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/seleniumpractice", "root", "Colb@121");
Statement smt = con.createStatement(); //Returns statement to execute query
ResultSet rs = smt.executeQuery("select FIRSTNAME from employeeInfo");
while(rs.next()) {
System.out.println(rs.getString("FIRSTNAME"));
}
Commit: https://github.com/writetoprabha/APIAutomation/commit/a5dd04bb51242f8a85c7743fdb1800950faf218b
- Set the baseURI property of the RESTASSURED static class
Example:
RestAssured.baseURI = "http://restapi.demoqa.com/utilities/weather/city"; - Create the RequestSpecification object and assign it with RestAssured.Given() method.
Example:
RequestSpecification httpRequest = ResetAssured.Given(); - Create the Response object and assign it with requestSpecificationObject.request() method
Example:
Response response = httpRequest.Request(METHOD.GET, "/Hyderabad");
Note: "/Hyderabad" is the parameter passed in GET request - The Response of the request invoked can be accessed using the response object
Example:
System.out.println(response.getResponseBody());
Notes:
JSONObject requestPayload = new JSONObject();
requestPayload.put("FirstName", firstName);
requestPayload.put("LastName", lastName);
requestPayload.put("UserName", userName);
requestPayload.put("Password", password);
requestPayload.put("Email", emailId);
httpRequest.header("Content-Type", "application/json");
httpRequest.body(requestPayload.toJSONString());
import io.restassured.RestAssured.;
import static org.hamcrest.Matchers.;
DesiredCapabilities capabilities = new DesiredCapabilities();
//To tell Appium server whether to perform the automation on Android or iOS device, specify the framework name:
capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "uiautomator2");
//Name of the emulator in android studio
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Pixel_3a_XL_API_28");
//To tell Appium server which app to open:
capabilities.setCapability(MobileCapabilityType.APP, System.getProperty("user.dir") + "/apps/ApiDemos-debug.apk");
//Driver object creation to perform the automation. Takes 2 arguments -
AndroidDriver<AndroidElement> driver = new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.findElement(By.xpath("//android.widget.TextView[@text='Preference']")).click();
driver.findElement(By.xpath("//android.widget.TextView[@text='3. Preference dependencies']")).click();
driver.findElement(By.xpath("//android.widget.CheckBox[@resource-id='android:id/checkbox']")).click();
driver.findElement(By.xpath("//android.widget.TextView[@text='WiFi settings']")).click();
driver.findElement(By.xpath("//android.widget.EditText[@resource-id='android:id/edit']")).sendKeys("Some text goes here");
driver.findElement(By.xpath("//android.widget.Button[@text='OK']")).click()
TouchAction touch = new TouchAction(driver);
touch.longPress(LongPressOptions.longPressOptions().withElement(element(source)).withDuration(Duration.ofSeconds(2))).moveTo(element(destination)).release().perform();
touch.longPress(element(source)).moveTo(element(destination)).release().perform();
driver.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector()).scrollIntoView(text(\"Visibility\"));");
driver.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector()).scrollIntoView(text("Date Widgets"));");