Automation testing
Selenium and TestNG Overview
1. What is Automation Testing?
- Description: Automation testing is the process of using specialized software to test the functionality of a program automatically, as opposed to manually testing it.
- Example: Running a Selenium script to check the functionality of a login form on a website.
2. When to Switch to Automation Testing?
- Description: Automation is ideal when:
- Repetitive tasks need to be executed frequently.
- Regression tests need to be run after every new build.
- Large-scale systems need to be tested, requiring speed and consistency.
- Manual testing would be too slow or prone to human error.
3. Why Automation Testing? / Advantages
- Advantages:
- Faster execution of test cases.
- More reliable with less human error.
- Reusable test scripts for future testing cycles.
- Continuous integration support.
- Better test coverage (running tests across multiple platforms and configurations).
4. Disadvantages of Automation Testing
- Disadvantages:
- High initial setup cost and time to create automated tests.
- Maintenance overhead for test scripts when the application changes.
- Not suitable for one-time or short-term testing projects.
- Requires skilled testers to create and maintain automated scripts.
5. Automation Testing Tools
- Examples:
- Selenium
- QTP (QuickTest Professional)
- JUnit
- TestComplete
- Cucumber
Selenium
6. What is Selenium?
- Description: Selenium is an open-source framework for automating web applications for testing purposes. It provides support for multiple programming languages such as Java, C#, Python, and Ruby, and works across different browsers.
7. Why Selenium? / Advantages
- Advantages:
- Supports multiple browsers (Chrome, Firefox, Safari, Edge).
- Supports multiple programming languages (Java, Python, C#, Ruby, etc.).
- Open-source and free to use.
- Supports mobile testing with Selenium Grid or Appium.
- Integrates well with CI/CD tools like Jenkins.
8. What are its Versions?
- Versions:
- Selenium 1 (Selenium RC) — used to be the most common version but was later replaced.
- Selenium 2 — Integrated WebDriver, the current version.
- Selenium 3 — Official support for WebDriver, the new standard.
- Selenium 4 — Latest version with additional features such as better Grid functionality, improved mobile support, and W3C WebDriver standard compliance.
9. What OS, Browsers, and Programming Languages does Selenium Support?
- Supported OS: Windows, macOS, Linux
- Supported Browsers: Chrome, Firefox, Safari, Internet Explorer, Edge
- Supported Languages: Java, Python, C#, Ruby, JavaScript, Kotlin
Java-Selenium Architecture
10. Java-Selenium Architecture
- Description: The architecture of Selenium consists of:
- WebDriver: The main interface for interacting with the browser.
- Browser Driver: Bridges WebDriver and the browser (e.g., ChromeDriver, GeckoDriver).
- Operating System: OS-specific implementation to interact with the browser.
WebDriver Architecture
11. WebDriver Architecture
- Description: WebDriver follows the client-server architecture:
- The client is the Selenium WebDriver API.
- The server is the browser driver (e.g., ChromeDriver).
- WebDriver communicates directly with the browser via the driver, sending commands (such as click, type, etc.) to the browser.
Basic Selenium Program to Open and Close Browser
12. Basic Selenium Program Example
java
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class BasicSeleniumExample {
public static void main(String[] args) {
// Set path for ChromeDriver
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
// Initialize WebDriver and open browser
WebDriver driver = new ChromeDriver();
driver.get("https://www.example.com");
// Close the browser
driver.quit();
}
}
Runtime Polymorphism Program in Selenium
13. Runtime Polymorphism Example in Selenium
java
interface Browser {
void open();
}
class ChromeBrowser implements Browser {
public void open() {
System.out.println("Opening Chrome Browser");
}
}
class FirefoxBrowser implements Browser {
public void open() {
System.out.println("Opening Firefox Browser");
}
}
public class SeleniumTest {
public static void main(String[] args) {
Browser browser = new ChromeBrowser();
browser.open(); // Runtime Polymorphism
}
}
WebDriver Abstract Methods
14. WebDriver Abstract Methods
- Methods like
get()
,click()
,sendKeys()
, etc., are abstract methods in WebDriver that are implemented by browser-specific driver classes likeChromeDriver
,FirefoxDriver
, etc.
Locators
15. Locators in Selenium
- Locators are used to find elements on the web page.
- ID:
driver.findElement(By.id("elementId"));
- Name:
driver.findElement(By.name("elementName"));
- XPath:
driver.findElement(By.xpath("//tagname[@attribute='value']"));
- CSS Selector:
driver.findElement(By.cssSelector("cssSelector"));
- Class Name:
driver.findElement(By.className("className"));
- Link Text:
driver.findElement(By.linkText("linkText"));
- Partial Link Text:
driver.findElement(By.partialLinkText("partialText"));
- ID:
Xpath, Types, and Cases
16. XPath Types and Examples
- Absolute XPath:
/html/body/div
- Relative XPath:
//div[@id='header']
- XPath with Multiple Conditions:
//input[@name='username' and @type='text']
- XPath with contains():
//a[contains(text(), 'Sign Up')]
Handling Multiple Elements
17. Handling Multiple Elements
- Description: Use
findElements()
to get a list of elements.
java
List<WebElement> elements = driver.findElements(By.className("exampleClass"));
Handling Synchronization Issues (Implicit and Explicit Wait)
18. Implicit Wait
java
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
19. Explicit Wait
java
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("example")));
Handling Dropdowns
20. Handling Dropdown (Static and Dynamic)
- Static Dropdown: Use
Select
class for static dropdown.
java
Select dropdown = new Select(driver.findElement(By.id("dropdown")));
dropdown.selectByVisibleText("Option 1");
- Dynamic Dropdown: Manually handle elements as dynamic dropdowns are populated via JavaScript.
Keyboard and Mouse Actions
21. Handling Keyboard and Mouse Actions
java
Actions actions = new Actions(driver);
actions.moveToElement(driver.findElement(By.id("hoverElement"))).click().build().perform();
Taking Screenshot
22. Taking a Screenshot
java
File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenshot, new File("screenshot.png"));
Handling Disabled Element
23. Handling Disabled Element
- Description: You can use JavaScriptExecutor to interact with disabled elements, but it's generally not recommended to interact with them unless required.
Performing Scroll Down Action
24. Scroll Down Action
java
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(0, 1000)");
WebElement Interface Methods
25. WebElement Interface Methods
- Methods:
click()
,sendKeys()
,getText()
,getAttribute()
,isDisplayed()
, etc.
Handling Popups (Web-Based and Window-Based)
26. Handling Popups
- Web-based: Use
Alert
interface.
java
Alert alert = driver.switchTo().alert();
alert.accept(); // Accept the alert
- Window-based: Use
Robot
class for window popups.
Handling Frames
27. Handling Frames
java
driver.switchTo().frame("frameName");
Handling New Windows/New Tabs
28. Handling New Windows/Tabs
java
String parentWindow = driver.getWindowHandle();
Set<String> allWindows = driver.getWindowHandles();
for (String window : allWindows) {
if (!window.equals(parentWindow)) {
driver.switchTo().window(window);
}
}
Automation Frameworks
29. Automation Framework
- Description: A structured approach to automate testing with reusable code, reporting, and logging. Types include Keyword-driven, Data-driven, Hybrid, and Page Object Model (POM).
POM (Page Object Model)
30. POM (Page Object Model)
- Description: A design pattern that creates an object-oriented class for each web page. This class contains methods to interact with the elements on the page.
TestNG
31. TestNG
- Description: A testing framework inspired by JUnit, designed for test configuration and parallel execution.
32. Fetching TestNG Report
- Description: TestNG generates XML and HTML reports by default to show the results of test execution.
Batch Execution in TestNG
33. Batch Execution
- Description: You can execute multiple test cases or test classes in a batch using TestNG’s XML configuration file.
TestNG Flags and Annotations
34. TestNG Flags and Annotations
- Annotations:
@Test
,@BeforeTest
,@AfterTest
,@BeforeMethod
,@AfterMethod
, etc.
Assertions
35. Assertions in TestNG
- Description: Assertions are used to validate the test conditions.
assertTrue()
,assertFalse()
,assertEquals()
,assertNull()
36. Grouping Execution in TestNG
- Description: Grouping test methods to execute them together based on specific categories.
Data Parameterization in TestNG
37. Data Parameterization
- Description: Use
@DataProvider
to pass different sets of data to test methods.
java
@DataProvider(name="data")
public Object[][] data() {
return new Object[][] { { "data1" }, { "data2" } };
}
Parallel Execution in TestNG
38. Parallel Execution
- Description: TestNG allows parallel execution of tests using the
<suite>
tag in the XML configuration file.
Distributed Parallel Execution
39. Distributed Parallel Execution
- Description: Distributes tests across multiple machines in a grid for better scalability.
Cross Browser Parallel Execution
40. Cross Browser Parallel Execution
- Description: Selenium Grid or tools like SauceLabs allow tests to be executed in parallel across multiple browsers.
This comprehensive guide provides an overview of the most common topics related to Selenium, TestNG, and automation testing.