0% found this document useful (0 votes)
15 views7 pages

Selenium Java Automation Techniques

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views7 pages

Selenium Java Automation Techniques

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

Launch a Browser and Open a URL

Ques on: Write a program to open Chrome browser, navigate to h ps://[Link], and
print its tle.

Answer:

java

import [Link];

import [Link];

public class OpenWebsite {

public sta c void main(String[] args) {

WebDriver driver = new ChromeDriver();

[Link]("h ps://[Link]");

[Link]("Page Title: " + [Link]());

[Link]();

Concepts tested: basic WebDriver usage, setup, browser ini a on.

2. Locate Elements Using Different Strategies

Ques on: Write code to find elements using id, name, class, and xpath.

Answer:

java

WebDriver driver = new ChromeDriver();

[Link]("h ps://[Link]");

driver.findElement([Link]("username")).sendKeys("admin");

driver.findElement([Link]("password")).sendKeys("pass123");
driver.findElement([Link]("login-btn")).click();

driver.findElement([Link]("//a[contains(text(),'Logout')]")).click();

Concepts tested: locator strategies, DOM interac on.

3. Handle Alerts and Pop-ups

Ques on: Write Selenium code to handle an alert popup.

Answer:

java

Alert alert = [Link]().alert();

[Link]([Link]());

[Link](); // Click OK

// or [Link](); // Click Cancel

Concepts tested: use of Alert interface, synchroniza on, modal handling.

4. Perform Mouse Hover Ac ons

Ques on: Write a program to hover over an element and click a submenu.

Answer:

java

WebDriver driver = new ChromeDriver();

Ac ons ac ons = new Ac ons(driver);

WebElement menu = driver.findElement([Link]("products"));

ac [Link](menu).perform();

driver.findElement([Link]("New Arrivals")).click();

Concepts tested: Ac ons class usage for advanced user interac ons.

5. Handle Mul ple Windows or Tabs

Ques on: Write Selenium Java code to switch between windows.

Answer:
java

String parentWindow = [Link]();

Set<String> windows = [Link]();

for (String win : windows) {

if (![Link](parentWindow)) {

[Link]().window(win);

[Link]("Switched to: " + [Link]());

[Link]();

[Link]().window(parentWindow);

Concepts tested: window handles, driver context switching.

6. Implement Explicit Wait

Ques on: Write a code snippet using WebDriverWait for synchroniza on.

Answer:

java

WebDriverWait wait = new WebDriverWait(driver, Dura [Link](10));

WebElement loginBu on =
[Link] l(ExpectedCondi [Link]([Link]("login")));

loginBu [Link]();

Concepts tested: synchroniza on, condi onal waits.

7. Read Test Data from Excel

Ques on: Demo reading test data from Excel file using Apache POI.

Answer:

java

FileInputStream file = new FileInputStream("[Link]");


Workbook workbook = new XSSFWorkbook(file);

Sheet sheet = [Link]("LoginData");

Row row = [Link](0);

String username = [Link](0).getStringCellValue();

String password = [Link](1).getStringCellValue();

[Link](username + " | " + password);

[Link]();

Concepts tested: data-driven tes ng, external data file handling.

8. Take Screenshot Using Selenium

Ques on: Write Java code to capture screenshots on test failure.

Answer:

java

File screenshot = ((TakesScreenshot) driver).getScreenshotAs([Link]);

FileU [Link](screenshot, new File("error_screenshot.png"));

Concepts tested: TakesScreenshot, excep on handling, repor ng automa on.

9. Manage Frames

Ques on: Write Selenium code to work with mul ple frames.

Answer:

java

[Link]().frame("mainFrame");

driver.findElement([Link]("searchBox")).sendKeys("Books");

[Link]().defaultContent();

Concepts tested: frame handling, DOM scope management.

10. Handle Dynamic Elements

Ques on: How do you handle elements with dynamic IDs?


Answer:
Use XPath func ons contains(), starts-with():

java

driver.findElement([Link]("//input[contains(@id,'user')]")).sendKeys("tester");

Concepts tested: XPath proficiency, dynamic content automa on.

11. Apply Page Object Model (POM)

Ques on: Write a simple POM structure for a Login page.

Answer:

java

public class LoginPage {

WebDriver driver;

@FindBy(id = "username") WebElement username;

@FindBy(id = "password") WebElement password;

@FindBy(id = "loginBtn") WebElement loginBtn;

public LoginPage(WebDriver driver) {

[Link] = driver;

[Link](driver, this);

public void login(String user, String pass) {

[Link](user);

[Link](pass);

[Link]();

}
Concepts tested: POM design pa ern, object-oriented structuring.

12. Differen ate Between Hard vs So Asser ons

Ques on:
Demonstrate hard and so asser ons in TestNG.

Answer:

java

// Hard Asser on

[Link](pageTitle, "Dashboard");

// So Asser on

So Assert so Assert = new So Assert();

so [Link]([Link](), "Home Page");

so [Link]([Link]());

so [Link]();

Concepts tested: TestNG usage, test control flow.

13. Scroll Page Using JavaScriptExecutor

Ques on: Write code to scroll the webpage.

Answer:

java

JavascriptExecutor js = (JavascriptExecutor) driver;

[Link]("[Link](0,500)");

Concepts tested: JavaScriptExecutor interface, DOM interac on.

14. Implement Fluent Wait

Ques on: Write code for FluentWait to handle poll intervals.

Answer:
java

Wait<WebDriver> wait = new FluentWait<>(driver)

.withTimeout(Dura [Link](30))

.pollingEvery(Dura [Link](5))

.ignoring(NoSuchElementExcep [Link]);

WebElement element = [Link] l(driver -> driver.findElement([Link]("delayedBu on")));

[Link]();

Concepts tested: advanced waits, synchroniza on control.

15. Data-Driven Login Test

Ques on: Use TestNG + Excel to run login tests.

Answer:

java

@DataProvider(name = "loginData")

public Object[][] getData() { return new Object[][] {{"admin","1234"}, {"user","abcd"}}; }

@Test(dataProvider = "loginData")

public void loginTest(String user, String pass) {

LoginPage loginPage = new LoginPage(driver);

[Link](user, pass);

[Link]([Link]().contains("Dashboard"));

You might also like