­

Subscribe

About Me

My photo
Welcome to my corner of the internet! I’m a software engineer by day, mechanical engineer by degree, some-time traveler, music lover, and self-proclaimed motivational speaker by... well, any spare minute I can find. When I’m not busy debugging code, you can find me lost in a new place, jamming to a mix of Indian and Western music (yes, I’m the one who creates playlists that make no sense), or pondering the mysteries of the universe, from physics to geopolitics. Oh, and if you need some motivation or a good laugh, I’m your go-to person—I'm basically a walking TED talk. Stick around for random musings, technical rants, and possibly some okayish poetry ✍️!

Translate

Wednesday, February 12, 2025

Selenium Webdriver (Part 3) - Dropdown

 static dropdown


- works only for dropdowns having web element with select as tagName.

WebElement staticdropdown = driver.findElement(By.id("")); 
Select dropdown = new Select(staticdropdown);
dropdown.selectByIndex(3);
dropdown.selectByValue("XYZ");
dropdown.selectByVisibleText("XYZ");
dropdown.getFirstSelectedOption().getText()

 dynamic dropdown

// parent - child relationship xpath; comment one of the two

Here one location can be in both From and TO dropdowns so we need to consider the From and TO Weblocators as parents and then
add the location as child.
Example below:

driver.findElement(By.xpath("//div[@id='ctl00_mainContent_ddl_originStation1_CTNR'] //a[@value='BLR']")).click();
Thread.sleep(2000);
driver.findElement(By.xpath("//div[@id='ctl00_mainContent_ddl_destinationStation1_CTNR'] //a[@value='MAA']")).click();

ctl00_mainContent_ddl_originStation1_CTNR - From locaton dropdown
ctl00_mainContent_ddl_destinationStation1_CTNR - To location dropdown







Selenium Webdriver (Part 2) - Waits

 Types of Waits:

1. Implicit Wait

2. Explicit Wait

  1. Webdriver Wait
  2. Fluent Wait

Implicit Wait-

Pros-

1. Declared globally. Wait for a max of n number of seconds before you throw error.
2. Readable code.

Cons-
1. Performance cause issues
2. if implicit wait is set to 20 s say, then we cannot find which components are slowly loading.

Explicit Wait-

Pros-
1. Specific to that locator only.
2. Wait is applied at only targeted elements. So no performance issues.

Cons-
1. More written code and disturbs readability.


_____________________________________________________________________________


Explicit Waits are of two types:

   1. Webdriver Wait
   2. Fluent Wait

Both implement the Wait interface.
FluentWait doesn't have methods to use, so we need to create custom methods for it to use it.


________________________________________________________________________________

Syntax/examples:

1. Fluent Wait

Wait<Webdriver> wait = new FluentWait<Webdriver>();
.withTimeout(10,SECONDS)
.pollingEvery(2,SECONDS)
.ignoring(NoSuchElementException.class)

2. Implicit Wait

driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); // Duration.of(5) not working

3. Explicit Wait 

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("element_id")));
wait.until(ExpectedConditions.textToBePresentInElementLocated(By.className("message"), "Success"));
wait.until(ExpectedConditions.alertIsPresent());

4. Thread.sleep

Halts the code for n seconds.










          


Selenium Webdriver (Part 1) - Locators

Locators


  1. CSS Selectors
    1. attribute      -- tagName[attribute='value']
    2. className  -- tagName.className
    3. Id                -- tagName#Id
    4. parent to child traverse -- parentTagName childTagName
    5. nth child     -- tagName[attribute='value']:nth-child(index) 
    6. Regex Eg    -- "input[type*='pass']"
    7. Notes -
      1. child to parent traverse is not possible in CSS
      2. tagName.className --> here tagName is optional, we can write .className also but the className should be unique.
      3. class="submit signInBtn" --> here are 2 identifiers, we can choose any one of them.
  2. XPath
    1. attribute      -- //tagName[@attribute='value']
    2. parent to child traverse -- //parentTagName/childTagName
    3. index           -- //tagName[@attribute='value'][index]
    4. Regex Eg    -- //button[contains(@class,'submit')]
    5. parent to child to child traverse Eg -- //header/div/button[1]/following-sibling::button[1]
    6. parent to child to parent to grandParent to child traverse Eg -- //header/div/button[1]/parent::div/parent::header/a
    7. Notes - 
      1. customized Xpath - both the words in the class name must be used. Eg: //button[@class='submit signInBtn']
  3. ID, name, className 
    1. just the ID/name/className respectively.
  4. With text
    1. linktext
    2. normalized-space()='xyz'
    3. xpath Eg- //button[text()='Log Out']

Persistent Automation Testing Interview Questions (5+ YOE)

What is Serialization and Deserialization and how to perform it? WAP for changing d to D in "I am from India" WAP for counting no....