Actual and Expected text not matched

Sure, this kind of thing is quite common. At the end of the day though, we don’t really care where the options are being sourced from. At least not for this topic.

Sure, the following xpath should find the element you want:

//div[text()='Customer Reference Attribute']/following-sibling::div[contains(@class, 'dropdown')]//li[contains(@class, 'hover')]

Please test this xpath in your browser console by entering the following (after you’ve selected an option):

$x("//div[text()='Customer Reference Attribute']/following-sibling::div[contains(@class, 'dropdown')]//li[contains(@class, 'hover')]")

If you get the expected element back, then you’d create a Test Object with that xpath, then get the text from it as you usually would.

When I test the xpath in the browser console, I’m getting this:

Try adding an @class to the first predicate:

//div[@class='title' and text()='Customer Reference Attribute']/following-sibling::div[contains(@class, 'dropdown')]//li[contains(@class, 'hover')]

Sorry, same result in the console.

A few things:

1.) Are you working with any iframes?
2.) Have you made a selection already?
3.) Try testing the xpath in pieces, i.e. test this:

//div[@class='title' and text()='Customer Reference Attribute']

then this:

//div[@class='title' and text()='Customer Reference Attribute']/following-sibling::div[contains(@class, 'dropdown')]

etc.

  1. I don’t believe there’s any iframes.
  2. I have made a selection already
  3. These did return something:

//div[@class='title' and text()='Customer Reference Attribute']

//div[@class='title' and text()='Customer Reference Attribute']/following-sibling::div[contains(@class, 'dropdown')]

But nothing for this one:

//div[@class='title' and text()='Customer Reference Attribute']/following-sibling::div[contains(@class, 'dropdown')]//li[contains(@class, 'hover')]

Ok, so definitely no iframes if the partial xpaths are working.

At the moment you are testing the full xpath, does one of the <li> elements have a class=“hover” attribute?

1 Like

Ewww, no it doesn’t.

Oh my gosh, you’re going to kill me. So there’s only a class=“hover” when I’m adding the bill and making the selection. Once I save the bill and view it, the selection is displayed in the field, but there is NO class=“hover”.

HTML on the VIEW page which is where I want to check the value that I have already selected:

<div class="col-9  list-filter  field edited">
<div class="title">Customer Reference Attribute</div>
<div class="error-details-panel align-left"></div>
<input type="text" autocomplete="off" class="simple" id="-1509188399">
<div class="dropdown panel dropup" style="display: none;">
<ul>
<li class="">ACCESSORY</li>
<li class="">BOL</li>
<li class="">BOL NUMBER</li>
<li class="">CASES</li>
<li class="">CUSTOMER CODE</li>
<li class="">CUSTOMER NUMBER</li>
<li class="">CUSTOMER REF</li>
<li class="">DEL ORD</li>
<li class="">GL CODE ID</li>
<li class="">IB PO</li>
<li class="">INBOUND PO</li>
<li class="">LANE NUMBER</li>
<li class="">LEDGER</li>
<li class="">LOAD NUMBER</li>
<li class="">LOB</li>
<li class="">LOCATION</li>
<li class="">MONDAYCRAB</li>
<li class="">ORDER NUMBER</li>
<li class="">OTHER NUMBER ONE</li>
<li class="">OTHER NUMBER TWO</li>
<li class="">PAK NUMBER</li>
<li class="">PALLET NUMBER</li>
<li class="">PC COUNT</li>
<li class="">PLANT CODE</li>
<li class="">PLANT LOCATION</li>
<li class="">PO NUMBER</li>
<li class="">QACRABONE</li>
<li class="">QACRABTHREE</li>
<li class="">QACRABTWO</li>
<li class="">RA NUMBER</li>
<li class="">RAILER</li>
<li class="">Reference Number</li>
<li class="">SALES ORDER NO</li>
<li class="">SHIPMENTID</li>
<li class="">SO Number</li>
<li class="">TEST</li>
</ul>
</div></div>

I have QACRABTWO selected currently.

Ewww is right, back to square one :upside_down_face:

Let me think on it…

Alright this is extremely hacky, but we could possibly get the value by copying it to the clipboard, then extracting the value from there:

1.) Locate the element, then use sendKeys to simulate copying the value to your clipboard:

WebDriver driver = DriverFactory.getWebDriver();
WebElement element = driver.findElement(By.xpath("//div[@class='title' and text()='Customer Reference Attribute']/following-sibling::input"));
element.sendKeys(Keys.chord(Keys.LEFT_CONTROL, 'a'));
element.sendKeys(Keys.chord(Keys.LEFT_CONTROL, 'c'));

2.) Get the current value from the clipboard using the java awt toolkit:

String value = (String) Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.stringFlavor); System.out.println(value)

You will need the following import statements for the above code to work:

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import java.awt.HeadlessException;
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;

I’ve never tried this personally, just throwing stuff at the wall to see what sticks… :expressionless:

1 Like

Another Tips & Tricks article in the making…

It failed at this line:

WebElement element = driver.findElement(By.xpath("//div[@class='title' and text()='Customer Reference Attribute']/following-sibling::input"));

2020-03-16 13:44:34.235 DEBUG testcase.CRAB02 - Add Bill with CRABs - 86: element = driver.findElement(By.xpath(“//div[@class=‘title’ and text()=‘Customer Reference Attribute’]/following-sibling::input”))
2020-03-16 13:44:34.257 ERROR c.k.k.core.main.RawTestScriptExecutor - :x: Test Cases/CRABS/CRAB02 - Add Bill with CRABs FAILED.
Reason:
groovy.lang.MissingPropertyException: No such property: driver for class: Script1584361548992
at CRAB02 - Add Bill with CRABs.run(CRAB02 - Add Bill with CRABs:189)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
at com.kms.katalon.core.main.RawTestScriptExecutor.runScript(RawTestScriptExecutor.java:34)
at com.kms.katalon.core.main.RawTestScriptExecutor.doExecute(RawTestScriptExecutor.java:29)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:307)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:299)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:233)
at com.kms.katalon.core.main.TestCaseMain.runTestCaseRawScript(TestCaseMain.java:151)
at com.kms.katalon.core.main.TestCaseMain$runTestCaseRawScript$0.call(Unknown Source)
at TempTestCase1584380654016.run(TempTestCase1584380654016.groovy:295)

2020-03-16 13:44:34.269 INFO c.k.k.core.main.RawTestScriptExecutor - END Test Cases/CRABS/CRAB02 - Add Bill with CRABs

The line just above it was this:

WebDriver driver = DriverFactory.getWebDriver();

which should have given you a driver to work with. Just to clarify, this code would only work after a WebUI.openBrowser() call earlier in the script (which adds an active driver instance to the DriverFactory).

Yes, that’s correct, I am calling a login test case and had it in the wrong place.

I do believe that was it! I’m getting the correct value verified. Thank you so much!

The complete solution is below for those who need the same help as I did and have stumbled across this thread:

The following import statements:

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import java.awt.HeadlessException;
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;

Include this:

WebUI.openBrowser('')
WebDriver driver = DriverFactory.getWebDriver()

Code to copy value and verify:

WebElement element = driver.findElement(By.xpath('//div[@class=\'title\' and text()=\'Customer Reference Attribute\']/following-sibling::input'))
element.sendKeys(Keys.chord(Keys.LEFT_CONTROL, 'a'))
element.sendKeys(Keys.chord(Keys.LEFT_CONTROL, 'c'))
value = ((Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.stringFlavor)) as String)
System.out.println(value)
WebUI.verifyMatch(value, findTestData('CRABs').getValue(1, 1), false)
2 Likes

Wow, I’m shocked it worked TBQH…

Glad you are up and running!

But it’s so ugly… I hate hacks…