Retrieval of corresponding values

Hi,
I wanted to retrieve the date 05/05/17 value by searching with the value 214 value as per the attachment. That means it should search in the page and identify the value and get the corresponding value of that. How I can do with the Groovy ? How to do with xpath ?
Please suggest to proceed further.

Please find the attached file for html code and screen shot of the web page.

html code1.txt (1.0 MB)

Hi vdavuluri,

Dealing with a list is not quite simple, you need to observe and be aware of the common locators of the items or row. It will be a lot of explanations.

To make it short, I suggest getting the xpath of the whole row or the attribute/locators that are common to them. Use them as your test object.

To give you an idea, refer to this code. . .

//import WebElement
import org.openqa.selenium.WebElement

//create a list
//this line will be the storing location of your items once it becomes visible to your website.
java.util.List<WebElement> _rowCommonLocator = WebUiCommonHelper.findWebElements(findTestObject("Common_Locator_of_the_rows"), 30)

//use a loop to go through all the row items
for(int rowIndex = 0; rowIndex < _rowCommonLocator.size(); rowIndex++)
{
    /**this if condition here will decide if the row contains "214" and "05/05/17"
        if not then it will continue to loop.
    **/
	if(_rowCommonLocator[rowIndex].text.contains("214")) &&
	   _rowCommonLocator[rowIndex].text.contains("05/05/17")
	  {
         if(_rowCommonLocator[rowIndex].text.contains("REVIEW"))
         {
                //since the characters of the rows are consistent, play with substring and eliminate unnecesary text to get your desired text output
                println _rowCommonLocator[rowIndex].text.substring(int beginIndex, int endIndex)
                break
         }
         else
         {
               //another substring value if the row doesnt contain "REVIEW" or "OPEN"
               break
         }	
      }
}

Hope that helps. . . :slight_smile:

1 Like

Arnel,
Thank you so much for your clarifications.
What should be given after the findTestObject ? what needs to be captured here or do we need mention about the xpath details here ?

java.util.List<WebElement> _rowCommonLocator = WebUiCommonHelper.findWebElements(findTestObject(“Common_Locator_of_the_rows”), 30)

Please clarify.

Hi vdavuluri,

Sorry I didn’t explained it much on my last reply. . .

Regarding this. . . . .

What I mean is inspect the whole row and get the xpath (Make the browser inspector highlight the whole row then get the xpath, right click and copy xpath) or find common attribute like class, name, title, etc. . . .

Then create a test object using the captured xpath or attribute then that will be your findTestObject(“Common_Locator_of_the_rows”). After successfully performing that step, print the _rowCommonLocator as said in my last reply. You will get an array output of:

[110OPEN05/07/2013GS00T07NSD0007IIT05/08/2013 04:00:00 PM, 204OPEN02/02/2017GS00T07NSD0007SFS02/02/2017/ 04:00:00 PM, and so on. . . . . . . .]

That means you have successfully captured all rows in the list, then after that refer to the code I provided. . .

Hope that makes sense. . . :slight_smile:

1 Like

Appreciate your quick help, Arnel.
Please find the attachments and mentioned about the question in each snap. Can you please look into this and suggest to proceed further ?

THANK YOU.

Hi vdavuluri,

As I mention. . . . .

See that “bgRow1”, “bgRow2” ??? They are common right??? Then use that as your locator… Create a new test object using that class attribute, but remove the number at the last part for them to become common and in order for the code to look for those items and be stored in your array list.

Then. . . . .

java.util.List<WebElement> lists = WebUiCommonHelper.findWebElements(findTestObject("Object Repository/Rows"), 30)

It will be very hard for you if you do not play with it. As I said it’s not easy dealing with lists. . .
Expand this idea. Not all list contains the same information or attribute inside. . .

Hope that helps. . . .

Thank you, Arnel.
I am testing to see what data will be stored in the element and I encountered the below error for below step definition. Can you please just give idea how to I keep it in xpath and it does not need the exact answers. Small guidance to proceed further. THANKS FOR YOUR HELP.

Step Definition:
@And(“user should verify that assigned tracking number displayed with resultsass”)
def should_verify_assigned_tracking_number_disp_results() {
if(runIndicator4!=‘N’) {
WebElement element = WebUiCommonHelper.findWebElement(By.xpath(“//a[starts-
with(@tr=‘bgRow’)]”,30))
println("Number of elements: " + element.size());
WebUI.delay(10)
}
}

Error from Log:

2019-07-11 18:50:15.744 ERROR c.k.k.c.c.keyword.CucumberReporter - :x: user should verify that assigned tracking number displayed with resultsass FAILED.
Reason:
groovy.lang.MissingMethodException: No signature of method: static org.openqa.selenium.By.xpath() is applicable for argument types:
(java.lang.String, java.lang.Integer) values: [//a[starts-with(@tr=‘bgRow’)], 30]
Possible solutions: xpath(java.lang.String), wait(), any(), wait(long, int), name(java.lang.String), getAt(java.lang.String)
at AssignInquiry004.should_verify_assigned_tracking_number_disp_results(AssignInquiry004.groovy:198)
at ✽.user should verify that assigned tracking number displayed with resultsass(C:/Users/vdavuluri2/Katalon Studio/Govt Inquiry/Include/features/Assign Inquiry-004.feature:26)

Hi vdavuluri,

Maybe this tip will help you. Take time to read. . .

Hope that helps you. . . :slight_smile:

1 Like

Yes… It is really helped, Arnel. Thank You so much.
Below is my code and it is working fine almost. The question is now, I will have to check for the class “bgRow2”. So how do I check both the classes in single line or regular expression. Only this clarification is pending. THANKS

CODE
@When(“user click on Bulk Assignment linkass”)
def user_click_on_bulk_assignment_link() {
WebDriver driver2 = DriverFactory.getWebDriver()
WebElement Table2 = driver2.findElement(By.xpath("//table[@id=‘mTAB1’]/tbody"))
List rows_table2 = Table2.findElements(By.className(“bgRow1”))
int rows_count2 = rows_table2.size()

	   for(int j=0; j < rows_count2; j++)
		 {
		   List<WebElement> cols2 = rows_table2.get(j).findElements(By.tagName('td'))
		   int cols_count2 = cols2.size()
		   println " cols_count2 in each row2 " + cols_count2
		   println " rows_table2.get(j) " + rows_table2.get(j)
		   for (int colum2 = 0; colum2 < cols_count2; colum2++)
			   {
                                                                       if(colum2==6)
			      {
			       valstore[intal] = cols2.get(colum2).getText()
				   String celltext2 = cols2.get(colum2).getText()
			       println "celltext2  " + celltext2
				   intal = intal + 1
                                                                               }
				if(j==4) j=1000;  
			   }
		 }
	   
	   /** CLICK ON BULK ASSIGN **/
	   WebUI.click(findTestObject('Object Repository/ORTC04/Page_Govt Inquiry/a_Bulk Assign'), FailureHandling.STOP_ON_FAILURE)
	   WebUI.delay(5)
	}
}

And also…
I see the below error when I execute the below mentioned step definition…

Step Definition
@When(“user click on Bulk Assignment linkass”)
def user_click_on_bulk_assignment_link() {
WebDriver driver2 = DriverFactory.getWebDriver()
WebElement Table2 = driver2.findElement(By.xpath("//table[@id=‘mTAB1’]/tbody"))
List rows_table2 = Table2.findElements(By.className(“bgRow1”))
int rows_count2 = rows_table2.size()

	   for(int j=0; j < rows_count2; j++)
		 {
		   List<WebElement> cols2 = rows_table2.get(j).findElements(By.tagName('td'))
		   int cols_count2 = cols2.size()
		   println " cols_count2 in each row2 " + cols_count2
		   println " rows_table2.get(j) " + rows_table2.get(j)
		   for (int colum2 = 0; colum2 < cols_count2; colum2++)
			   {
                                                                       if(colum2==6)
			      {
			       valstore[intal] = cols2.get(colum2).getText()
				   String celltext2 = cols2.get(colum2).getText()
			       println "celltext2  " + celltext2
				   intal = intal + 1
                                                                               }
				if(j==4) j=1000;  
			   }
		 }
	   
	   /** CLICK ON BULK ASSIGN **/
	   WebUI.click(findTestObject('Object Repository/ORTC04/Page_Govt Inquiry/a_Bulk Assign'), FailureHandling.STOP_ON_FAILURE)
	   WebUI.delay(5)
	}
}

Error Log
rows_count2 10
cols_count2 in each row2 44
rows_table2.get(j) [[[[CChromeDriver: chrome on XP (c9105cad763f08278655b121b95ef3f9)] -> xpath: //table[@id=‘mTAB1’]/tbody]] -> class name: bgRow1]
2019-07-12 20:53:12.083 ERROR c.k.k.c.c.keyword.CucumberReporter - :x: user click on Bulk Assignment linkass FAILED.
Reason:
java.lang.IllegalArgumentException: String of length 1 expected but got a bigger one
at AssignInquiry004.user_click_on_bulk_assignment_link(AssignInquiry004.groovy:270)
at ✽.user click on Bulk Assignment linkass(C:/Users/vdavuluri2/Katalon Studio/Govt Inquiry/Include/features/Assign Inquiry-004.feature:28)

Hi vdavuluri,

What do you mean by this?

Is it do you want to check the value of the class??

If so, then use getAttribute method of WebElement. . .

String attributeValue = yourList[index].getAttribute("class")

Also if you want to include “bgRow2” in your list, remove the number from the last part. It should only be “bgRow” inorder for your list to capture all the items in your list. . .

As you can see in your previous reply, the first screenshot you have sent, all of them are “bgRow1” and yet you still use “bgRow1”. So it will just get all the “bgRow1” and “bgRow2” will be left behind. That’s why you need to remove the number from the last part to make all items the same so that your list can capture them all.

It should be like this:

List rows_table2 = Table2.findElements(By.className(“bgRow”))

:slight_smile:

Thanks for the detailed steps, Arnel.
Question : 1
Yes… As I showed in earlier, it has one row with “bgRow1” and another row with “bgRow2”. So, to capture both the classes, asked about the how to track the last digit. When I tried with
List rows_table2 = Table2.findElements(By.className(“bgRow”)), it is not not identifying any classname. Do I need to keep any “?” or other to track in the last digit ?

Question : 2
With getAttribute, will I get the classname ? what should be given for yourList(index) ?
String attributeValue = yourList[index].getAttribute(“class”)

Question : 3
When I try to execute the below step definition, getting the attached error at the below highlighted code with array - valstore[intal] = cols2.get(colum2).getText(). I have verified at the debugging mode. It is looking for io.cucumber.core_3.0.2.jar file.

Step Definition
@When(“user click on Bulk Assignment linkass”)
def user_click_on_bulk_assignment_link() {
WebDriver driver2 = DriverFactory.getWebDriver()
WebElement Table2 = driver2.findElement(By.xpath(“//table[@id=‘mTAB1’]/tbody”))
List rows_table2 = Table2.findElements(By.className(“bgRow1”))
int rows_count2 = rows_table2.size()

for(int j=0; j < rows_count2; j++)
{
List cols2 = rows_table2.get(j).findElements(By.tagName(‘td’))
int cols_count2 = cols2.size()
println " cols_count2 in each row2 " + cols_count2
println " rows_table2.get(j) " + rows_table2.get(j)
for (int colum2 = 0; colum2 < cols_count2; colum2++)
{
if(colum2==6)
{
valstore[intal] = cols2.get(colum2).getText()
String celltext2 = cols2.get(colum2).getText()
println "celltext2 " + celltext2
intal = intal + 1
}
if(j==4) j=1000;
}
}

   /** CLICK ON BULK ASSIGN **/
   WebUI.click(findTestObject('Object Repository/ORTC04/Page_Govt Inquiry/a_Bulk Assign'), FailureHandling.STOP_ON_FAILURE)
   WebUI.delay(5)
}

}

Hi. . .

For question 1:
Sorry I was wrong about this:

List rows_table2 = Table2.findElements(By.className(“bgRow”))

It should be contains:

List rows_table2 = Table2.findElements(By.xpath(“//*[contains(@class, 'bgRow')]”))

For question 2:

  • You will get the value of the classname. e.g. yourList[index].getAttribute(“class”), it will return the classname of the specified index.
    Sample Code:
WebDriver driver2 = DriverFactory.getWebDriver()
WebElement Table2 = driver2.findElement(By.xpath("//table[@id=‘mTAB1’]/tbody"))
List rows_table2 = Table2.findElements(By.Xpath("//*[contains(@class, 'bgRow')]”))
int rows_count2 = rows_table2.size()

String attributeValue = rows_table2[SampleIndex].getAttribute("class")

For question 3:

  • I don’t have any idea for that, sorry.

Hope that answer your questions. . . .

Thank You so much, Arnel. Thanks for your inputs.