0

I am writing test cases using Selenium and PhantomJsDriver in Java

  • Selenium - 3.0.1

  • PhantomJs - 2.1.1

Expected Scenario : Open a pop-up page and find the No of elements inside the pop up page (Actually the items getting displayed inside the pop up).

At any given point of time there can be only 3 elements inside the pop up. So i am doing an assert here.

Below is the Code for the same

  1. With Class Name using findElements method

    List<WebElement> foundItems = By.className("className").findElements(driver);
    
    int count = foundItems.size();
    
  2. With Xpath

    int count = driver.findElements(By.xpath("//div[@class='className']")).size();
    

In both the cases i am getting the count wrong, i always get the count as multiple of elements which are inside the pop up page.

But if i iterate over the list and use .isDisplayed() method and maintain a flag it is giving me the correct count.

I think it might be an issue of cache or localStorage issue which phantomJsDriver maintains.

How could i clear the Cache or LocalStorage using Selenium and Java.

Or is there any other way to get it done.

NarendraR
  • 7,577
  • 10
  • 44
  • 82
Jay
  • 429
  • 2
  • 8
  • 23

2 Answers2

0

You should provide HTML of your page so we could help you.

As for the local storage, you can delete it using javascript, this should work fine:

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.localStorage.clear();");
acikojevic
  • 915
  • 1
  • 7
  • 16
  • So there might be previous counts maintained by the pop up page as local storage ? If i use the JavascriptExecutor will it clear of all those and get the fresh count in the recentely openly pop up. – Jay Jan 21 '17 at 08:09
0

The answer depends on two things:

  1. What exactly you are trying to get the count of
  2. What method the page uses to hide controls

In my example below, I show an XPath that will show the number of input controls excluding those designated as type 'hidden'

driver.findElements(By.xpath("//input[not(@type='hidden')]")).size();

On a page I tested, //input returned 6 items, but I only saw four. Adding the extra qualifier brought the number found to four.

Alternatively, you can get the count of each type you are looking for, and accumulate the numbers, i.e.:

int eleCount = driver.findElements(by.xpath("//input")).size() +
               driver.findElements(by.xpath("//a")).size() +
               driver.findElements(by.xpath("//h1")).size();

Would return the numbers of input elements, anchors, and headers correspondingly. If you need to know more, then check for more tag names.

Bill Hileman
  • 2,798
  • 2
  • 17
  • 24