Verify 2 web elements in table are equal or not - selenium

List<WebElement> tdata=driver.findElements(By.tagName("tr"));
List<WebElement> tdata1=driver1.findElements(By.tagName("tr"));
for(int i=0,j=0; i<tdata.size() && j<tdata1.size();i++,j++ )
{
WebElement row = tdata.get(i);
WebElement row1 = tdata.get(j);
System.out.print(row1.getText());
System.out.print(row1);
if(row.getText().equals(row1.getText()))
{
System.out.println(row.getText());
}
else if(!(row.getText().equals(row1.getText())))
{
System.out.print("Not matching text");
System.out.println(row1.getText());
}
}
This is my code for comparing 2 web table, I am Unable to verify content equal or not equal. For unmatched text also it is not printing anything. else if part is not capturing if elements are not equal.

try
List<WebElement> tdata= ...
List<WebElement> tdata1= ...
int common= 0;
for (WebElement element: tdata)
if (tdata1.contains(element))
common++;

Related

Limit the element to be stored in List<WebElement> - selenium/java

I need to know how to store the first 10 values in List<WebElement> instead of storing all the elements that is present?
Right now my code stores all the elements:
By mySelector = By.xpath("/html/body/div[1]/div/section/div/div[2]/form[1]/div/ul/li");
List<WebElement> myElements = driver.findElements(mySelector);
for(WebElement e : myElements) {
System.out.println(e.getText());
}
Try using position() with the li.
By.xpath("/html/body/div[1]/div/section/div/div[2]/form[1]/div/ul/li[position() < 11]")

Dropdown duplicate value automation using Selenium

How to check the duplication of values in a checkbox using Selenium Webdriver
something like the below one will work if both the options have same value
public boolean isSelectOptionsRepeating(WebElement dropdown)
{
Select s = new Select(dropdown);
List<WebElement> list = s.getOptions();
Set<String> listNames = new Hashset<String>(list.size());
for (WebElement w : list) {
listNames.add(w.getText().trim());
}
if(list.size()== listNames.size())
return true;
else
return false;
}
You can store the values of drop down in String array and
traverse string array and use Hashmap for storing the values from the dropdown and if duplicate occurs increement the count by one
voila......you would know the the Values with its count, if count > 1. Duplicate
for reference : Java Beginner - Counting number of words in sentence

Selecting Date on a Page using selenium Webdriver

This is the website I am testing https://www.sydneyairport.com.au/go/car-parking.aspx , I am almost done , but have been stuck in a single issue .
I have selected "date and time" from entry-date section but cannot select "date and time" from exit-date section.
I am clueless why I am not able to it since its the same structure for both and I was able to do it for entry-date ,I don't understand what changes for exit date section . I am new to selenium and will appreciate if anybody helps me out .
This is what I have written to select date and time in entry date section .
public void selectDate(WebDriver driver, String fromDate, String toDate) {
// selects from date
WebElement dateButton = driver.findElement(By.id("period_picker_0"));
dateButton.click();
WebElement datepicker = driver.findElement(By.xpath("//div[#class='period_picker_days']/table/tbody/tr/td[1]"));
selectDate(datepicker, fromDate);
WebElement timeBox = driver.findElement(By.xpath("//div[#class='period_picker_work']/div[2]/input"));
timeBox.sendKeys("");
WebElement time = driver
.findElement(By.xpath(".//*[#id='timepicker_box_start']/div/div[2]/div/div[1]/div[13]"));
time.click();
// Selects to date
WebElement dateButton2 = driver.findElement(By.id("period_picker_1"));
dateButton2.click();
// dateButton.click();
WebElement datepicker2 = driver
.findElement(By.xpath("//div[#class='period_picker_days']/table/tbody/tr/td[2]"));
selectDate(datepicker2, toDate);
WebElement timeBoxEnd = driver.findElement(By.xpath("//div[#class='period_picker_work']/div[2]/input"));
timeBoxEnd.sendKeys("");
WebElement timeEnd = driver
.findElement(By.xpath(".//*[#id='timepicker_box_end']/div/div[2]/div/div[1]/div[13]"));
timeEnd.click();
}
public int selectDate(WebElement datepicker, String date) {
int ele = 0;
List<WebElement> rows_table = datepicker.findElements(By.tagName("tr"));
int rows_count = rows_table.size();
for (int row = 0; row < rows_count; row++) {
// To locate columns(cells) of that specific row.
List<WebElement> Columns_row = rows_table.get(row).findElements(By.tagName("td"));
// To calculate no of columns(cells) In that specific row.
int columns_count = Columns_row.size();
// Loop will execute till the last cell of that specific row.
for (int column = 0; column < columns_count; column++) {
// To retrieve text from that specific cell.
if (Columns_row.get(column).getText().equals(date)) {
ele = column;
Columns_row.get(column).click();
}
}
}
return ele;
}
I did lot of work for this code, You are using tr and td. I would suggest there is no need of it.
See the code I'm using, i am able to select both date easily. Hope this help you..
driver.get("https://www.sydneyairport.com.au/go/car-parking.aspx");
driver.manage().window().maximize();
driver.findElement(By.xpath(".//*[#id='period_picker_0']")).click();
Actions a = new Actions(driver);
WebDriverWait wait = new WebDriverWait(driver,20);
WebElement entrydate= driver.findElement(By.xpath(".//*[#id='body']/div[1]/div[2]/div[1]/table/tbody/tr/td[1]/table/tbody/tr[5]/td[5]"));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[#id='body']/div[1]/div[2]/div[1]/table/tbody/tr/td[1]/table/tbody/tr[5]/td[5]")));
a.moveToElement(entrydate).build().perform();
Thread.sleep(5000L);
entrydate.click();
WebElement entrytime= driver.findElement(By.xpath(".//*[#id='timepicker_box_start']/div/div[2]/div/div[1]/div[15]"));
a.moveToElement(entrytime).build().perform();
Thread.sleep(5000L);
entrytime.click();
WebElement exitdate= driver.findElement(By.xpath(".//*[#id='body']/div[2]/div[2]/div[1]/table/tbody/tr/td[1]/table/tbody/tr[6]/td[5]"));
a.moveToElement(exitdate).build().perform();
Thread.sleep(5000L);
exitdate.click();
WebElement exittime= driver.findElement(By.xpath(".//*[#id='timepicker_box_end']/div/div[2]/div/div[1]/div[15]"));
a.moveToElement(exittime).build().perform();
Thread.sleep(5000L);
exittime.click();
Please do reply, how you find this and reply back to me for further query.
Happy Learning. :-)

Incrementing value with Selenium IDE

How do I increment value of img path when said path looks like this?
//ab[x]/img
X value increasing by 1 and has a limit of 50.
Trying to write a test case on how to click on several images on website.
Edit: Just wanted to add that I'm just starting with Selenium IDE and using standart commands.
Solution 1: Format your xpath path selector
for(int i=1; i<=numberOfImages; i++) {
String path = String.format("//ab[%d]/img", i);
WebElement image = driver.findElement(By.xpath(path));
if(image != null) {
image.click();
}
}
Solution 2: Select all elements that "//ab/img" returns and iterate over them.
String path = "//ab/img";
List<WebElement> imgElements = driver.findElements(By.xpath(path)); //notice the plural
for(WebElement image : imgElements) {
image.click();
}

Count number of elements that has a score

Is there a way to count the number of elements that isn't equal to 0.00?
For example, the code is
<div id="average_2123" style="font-size:20px; ">0.00</div>
<div id="average_2124" style="font-size:20px; ">23.53</div>
<div id="average_2125" style="font-size:20px; ">0.00</div>
How can I count the element so it's only 1 since only one of them has a score?
I want to do this on PHPUnit. I can also do it on Selenium IDE because I can convert it to PHPUnit
you will have to write custom code. I am writing java pseudocode. hope you can understand and convert
List<WebElements> ElemList = Webdriver.FindElements(By.Xpath("//div")
for (i = 0; i < ElemList.size();i++)
{
WebElement Current =List.getElementAt(i);
String ElemName = current.getAttribute("id");
String text =""
int Count = 0;
if( id.Contains("average"))
{
if( !id.getAttribute("value").equals("0.00")
{
count++;
}
}
}
A better approach can be as mentioned below. Im writing the code in Java:
List<WebElement> elemList = driver.findElements(By.cssSelector("div[id^='average']"));
List<WebElement> filteredElements = new ArrayList<WebElement>();
for (WebElement element : elemList) {
if (Long.parseLong(element.getText()) > 0.00)
filteredElements.add(element);
}
This will be find all the elements whose "id" attribute starts with "average".
Also here i am converting the text to long and then comparing whether its grater than 0.00
The filteredEleemnts are the elements which have value greater than 0.00