0% found this document useful (0 votes)
2 views

Web Automation

This document provides a comprehensive guide on Selenium WebDriver, covering its setup, commands for browser interaction, locators, and methods for handling web elements, alerts, and frames. It also discusses automation frameworks, test case selection, writing maintainable code, common exceptions, and data-driven testing. Additionally, it includes information on TestNG annotations, assertions, and test reporting.

Uploaded by

ahsan habib
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Web Automation

This document provides a comprehensive guide on Selenium WebDriver, covering its setup, commands for browser interaction, locators, and methods for handling web elements, alerts, and frames. It also discusses automation frameworks, test case selection, writing maintainable code, common exceptions, and data-driven testing. Additionally, it includes information on TestNG annotations, assertions, and test reporting.

Uploaded by

ahsan habib
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Web Automation

Created by Abdur Rahmam Foysal


Linkedin: https://www.linkedin.com/in/arfoysal/
Introduction to Selenium WebDriver
● Overview of Selenium WebDriver
○ Browser automation tool for web applications.
○ Cross-browser compatibility (Chrome, Firefox, Edge).
● Setting Up Selenium WebDriver Environment
○ Install Java, Maven & IDE (Eclipse/IntelliJ).
○ Create a Maven project
○ Add Selenium WebDriver dependencies (Maven).
● Basic Selenium WebDriver Script
WebDriver driver = new ChromeDriver();
driver.get("https://example.com");
driver.close();
● Configure Browser Setup Class
Interacting with Web Browser
● Get Command driver.get("url")
● Get Current URL Command driver.getCurrentUrl()
● Get Title Command driver.getTitle()
● Minimize window size driver.manage().window().minimize()
● Maximize window size driver.manage().window().maximize()
● Full screen window size driver.manage().window().fullscreen()
● Navigate to Command driver.navigate().to("url");
● Navigate Back Command driver.navigate().back();
● Navigate Forward Command driver.navigate().forward();
driver.navigate().refresh();
● Navigate Refresh Command
driver.switchTo().newWindow(WindowType.TAB)
● Create new tab
driver.switchTo().newWindow(WindowType.WINDOW)
● Create new window
driver.getWindowHandles()
● Switching windows or tabs driver.switchTo().window("WindowHandle")
● Close Command driver.close()
● Quit Command driver.quite()
Locators in Selenium
● ID Locator
driver.findElement(By.id("elementId"));
● Name Locator
driver.findElement(By.name("elementName"));
● LinkText Locator
driver.findElement(By.className("elClassName"));
● Partial LinkText Locator
driver.findElement(By.tagName("elementTagName"));
● Tag Name Locator
driver.findElement(By.cssSelector("cssSelector"));
● Class Name Locator
driver.findElement(By.xpath("elementXpath"));
● CSS Selector
driver.findElement(By.linkText("elementlinkText"));
● XPath Selector
driver.findElement(By.partialLinkText("partialLinkText"));
● Custom Xpath
● Custom CSS
Interacting with Web Elements
● Search
○ WebElement element =driver.findElement(By.id("elementId"));
○ List<WebElement> elements = driver.findElements(By.xpath("elementsXpath"));

● Get Text Command element.getText()


● Click Command element.click()
● Clear Command element.clear()
● SendKeys Command element.sendKeys()
● Get Attribute Command element.getAttribute("attributeName")
● Get CSS Value Command element.getCssValue("propertyName")
● Is Displayed Command element.isDisplayed()
● Is Enabled Command element.isEnabled()
● Is Selected Command element.isSelected()
Interacting with Dropdown (Select Tag)
● Search
○ WebElement el = driver.findElement(By.tagName("select"));
○ Select select = new Select(el);
● Get all options select.getOptions();
● Get first selected option select.getFirstSelectedOption();
● Get all selected options select.getAllSelectedOptions(); ● Deselect All select.deselectAll();
● Select By value select.selectByValue("value"); ● Deselect By value select.deselectByValue("value");
● Select By index select.selectByIndex(2); ● Deselect By index select.deselectByIndex(2);
● Select By visible text select.selectByVisibleText("Dhaka"); ● Deselect By visible text select.deselectByVisibleText("Dhaka");

Interacting with Alert


● Switch to an Alert Alert alert = driver.switchTo().alert();
● Get Alert text alert.getText();
● Accept an Alert alert.accept();
● Dismiss an Alert alert.dismiss();
● Write on Alert input box alert.sendKeys("Hello");
Interacting with Frame content
● Switching to a Frame
○ By Frame id drive.switchTo().frame("iframe_id");
○ By Frame name drive.switchTo().frame("iframe_name");
○ By Frame index drive.switchTo().frame(0);
○ By Frame Web Element drive.switchTo().frame(webElement);
● Returning from a Frame
○ Return to previous/parent frame driver.switchTo().parentFrame();
○ Return to default DOM driver.switchTo().defaultContent();

Waits in Selenium WebDriver


● Implicit wait
○ driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(15));
● Explicit wait - WebDriverWait wait = new WebDriverWait(browser, Duration.ofSeconds(15));
○ wait.until(ExpectedConditions.presenceOfElementLocated(locator));
○ wait.until(ExpectedConditions.elementToBeClickable(locator));
○ wait.until(ExpectedConditions.invisibilityOfElementLocated(locator));
○ wait.until(ExpectedConditions.visibilityOfElementLocated(loader_delay_text));
○ wait.until(ExpectedConditions.textToBePresentInElementLocated(locator,"Text" ))
○ wait.until(ExpectedConditions.alertIsPresent());
Actions-Emulating complex user gestures
Actions actions = new Actions(driver);

● Perform scroll by amount actions.scrollByAmount(0, 1000).build().perform()


● Perform scroll to element actions.scrollToElement(webElement).build().perform()
● Perform Hover actions.moveToElement(webElement).build().perform();
● Perform Click actions.click(webElement).build().perform();
● Perform Clcik and hold actions.clickAndHold(webElement).build().perform();
● Perform double click actions.doubleClick(webElement).build().perform();
● Perform Context click actions.contextClick(webElement).build().perform();
● Perform Drag and Drop click actions.dragAndDrop(sourceElement, targetElement).build().perform();
● Perform marked all actions.keyDown(Keys.CONTROL).sendKeys("a").build().perform();
● Perform Copy actions.keyDown(Keys.CONTROL).sendKeys("x").build().perform();
● Perform Pest actions.keyDown(Keys.CONTROL).sendKeys("v").build().perform();
TestNG Annotations
@BeforeSuite, @BeforeTest, @BeforeClass, @BeforeMethod

@Test: Define test cases.

@AfterMethod, @AfterClass, @AfterTest, @AfterSuite

Assertions
● Hard Assertion
○ Assert.assertEquals("one", "one");
○ Assert.assertTrue(true);
○ Assert.assertFalse(false);
● Soft Assertion - SoftAssert softAssert = new SoftAssert()
○ softAssert.assertEquals("one", "one");
○ softAssert.assertTrue(true);
○ softAssert.assertFalse(false);
○ softAssert.assertAll();
Automation Frameworks
A framework provides a structured environment for writing and executing test cases, ensuring consistency
and reusability across tests. It integrates tools, libraries, and best practices to streamline automation.

● Advantages of Using Frameworks:


○ Promotes code reusability and maintainability.
○ Simplifies test development and execution.
○ Enhances test scalability and organization.
○ Provides better reporting and error handling.
● Page Object Model (POM):
○ A design pattern where each web page is represented as a class. Elements are defined as variables, and
user actions are implemented as methods. It enhances maintainability by separating the test scripts
from UI elements.
Driver Setup for Framework
Selecting Good Test Cases for Automation
Not all test cases are suitable for automation. Ideal test cases include repetitive tests, high-risk areas, and
business-critical functionalities. Focus on tests that are stable, have predictable outcomes, and benefit from fast
execution, like regression testing, data-driven tests, or tests across multiple browsers. Avoid automating constantly
changing or one-time tests.

Writing Maintainable and Reusable Code


Maintainable and reusable code is key to scaling automation efforts efficiently. Custom methods allow you to
encapsulate commonly used actions (like logging in, filling forms, or navigating pages) into reusable functions. This
reduces duplication and improves readability, making your code easier to update and modify when application changes
occur. With well-structured custom methods, testers can write cleaner, modular scripts that are simple to understand
and maintain over time, while improving collaboration within teams. Reusability also leads to faster test creation by
avoiding repetitive code.
Selenium Exceptions
● NoSuchElementException: Thrown when the element you're trying to locate does not exist on the page.
● TimeoutException: Occurs when a command does not complete in the specified time.
● ElementNotVisibleException: Raised when an element is present in the DOM but not visible (i.e., it has display: none).
● ElementNotInteractableException: The element is found but cannot be interacted with (e.g., it's disabled or hidden).
● StaleElementReferenceException: Raised when an element is no longer attached to the DOM.
● NoSuchFrameException: Thrown when the frame you're trying to switch to doesn't exist.
● NoSuchWindowException: Raised when the window you're trying to switch to does not exist.
● InvalidElementStateException: Element is in a state that means it cannot be interacted with (e.g., typing into a checkbox).
● WebDriverException: General error occurs when interacting with the WebDriver.
● InvalidSelectorException: Thrown when the provided selector (XPath or CSS) is invalid.
● NoAlertPresentException: Thrown when trying to switch to an alert that is not available.
● JavascriptException: Raised when there is an issue executing JavaScript via executeScript().
● SessionNotFoundException: Occurs when the WebDriver session is not available (e.g., when the browser is closed).
● MoveTargetOutOfBoundsException: Raised when the target location for an action is outside the allowable area.
● InvalidArgumentException: Raised when an argument passed to a WebDriver command is invalid.
Data Driven Testing
Test Reporting
Creating Test Suites
Run test from Command Line
Problem Solving
Project Assaign

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy