I have recorded a script for my website, I have also added a csv/xls file with data and injected the variables into the script, The first row always works without any issues. However the second row does not play back the script correctly. How can I solve this issue?
Good day @abcd
Please read the following post so that we can assist you quicker and more efficiently
Thanks! Here is the full description of what I am trying to do
Here is the HTML
Medium Duty
When you click on the link, I want to select “Medium Duty” from the dropdown list (this is displayed as span. Also, is it possible to select this from a variable, so if the variable says light duty, I need to pick up light duty and so on.
My xpath is /html/body/div[1]/div/div/div/main/form/div[1]/div[2]/div[2]/div/button/i
Here is the URL for the site EV Calculator
I might be understanding a bit better now.
My understanding is that you want to click on an item in the dropdown based on the Text it contains and the Text it contains is stored in a variable. So that when the variable changes, it will click the corresponding dropdown item.
I would suggest the following:
The first line containing import needs to be included at the very top of the test case where the other imports are located
import com.kms.katalon.core.testobject.ConditionType as ConditionType
//Set Variable for the dropdown item text
String variable1 = 'Light Duty'
//Create a new TestObject
TestObject dropdownItem = new TestObject()
//Add Xpath to TestObject
String xpath = "//li[@value='" + variable1 + "']"
dropdownItem.addProperty('xpath', ConditionType.EQUALS, xpath)
//Click TestObject
WebUI.click(dropdownItem)
Here is the breakdown:
Step 1: I set a variable to the text of the dropdown item I am looking for
//Set Variable for the dropdown item text
String variable1 = 'Light Duty'
Step 2: I create a custom test object called ‘dropdownItem’
//Create a new TestObject
TestObject dropdownItem = new TestObject()
Step 3: I add the xpath to the dropdown test object containing the value of the dropdown item
It is crucial to include the import line at the top of the script mentioned above as this part depends on it
//Add Xpath to TestObject
String xpath = "//li[@value='" + variable1 + "']"
dropdownItem.addProperty('xpath', ConditionType.EQUALS, xpath)
Step 4: Click on the custom test object
//Click TestObject
WebUI.click(dropdownItem)
I have tested this on the link you provided and was able to select the dropdown value I wanted by changing the variable to the text in the dropdown item.
Also make sure you click on the dropdown first before trying to click on the dropdown item as the items needs to be visible before trying to click on it
If you are still having trouble, please include a screenshot of your test case and the errors you are getting
Thank you! I tried implementing the same for another dropdown and its giving an error. Here is my test script
Select “Medium or Large Businessess”
import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint
import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase
import static com.kms.katalon.core.testdata.TestDataFactory.findTestData
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import static com.kms.katalon.core.testobject.ObjectRepository.findWindowsObject
import com.kms.katalon.core.checkpoint.Checkpoint as Checkpoint
import com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords as CucumberKW
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobile
import com.kms.katalon.core.model.FailureHandling as FailureHandling
import com.kms.katalon.core.testcase.TestCase as TestCase
import com.kms.katalon.core.testdata.TestData as TestData
import com.kms.katalon.core.testng.keyword.TestNGBuiltinKeywords as TestNGKW
import com.kms.katalon.core.testobject.TestObject as TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import com.kms.katalon.core.windows.keyword.WindowsBuiltinKeywords as Windows
import internal.GlobalVariable as GlobalVariable
import org.openqa.selenium.Keys as Keys
import com.kms.katalon.core.testobject.ConditionType as ConditionType
WebUI.openBrowser(‘’)
WebUI.navigateToUrl(‘https://unbranded.fleets.d.zappyride.com/’)
WebUI.click(findTestObject(‘Object Repository/Page_PSE Fleets/span_Create Your First Vehicle Set’))
//Set Variable for the dropdown item text
String variable1 = ‘Medium Or Large Businesses’
//Create a new TestObject
TestObject dropdownItem = new TestObject()
//Add Xpath to TestObject
String xpath = “//li[@value='” + variable1 + “']”
dropdownItem.addProperty(‘xpath’, ConditionType.EQUALS, xpath)
//Click TestObject
WebUI.click(dropdownItem)
WebUI.setText(findTestObject(‘Object Repository/Page_PSE Fleets/input_Zip Code_zipcode’), ‘66014’)
WebUI.click(findTestObject(‘Object Repository/Page_PSE Fleets/span_Next’))
WebUI.setText(findTestObject(‘Object Repository/Page_PSE Fleets/input_Number of vehicles_vehicleCount’), ‘2’)
WebUI.click(findTestObject(‘Object Repository/Page_PSE Fleets/span_Next’))
WebUI.setText(findTestObject(‘Object Repository/Page_PSE Fleets/input_Average Business Miles Per Vehicle (P_801385’), ‘140’)
WebUI.setText(findTestObject(‘Object Repository/Page_PSE Fleets/input_Average Personal Miles (Per Day)_pers_8118a6’), ‘20’)
WebUI.click(findTestObject(‘Object Repository/Page_PSE Fleets/span_Next’))
WebUI.click(findTestObject(‘Object Repository/Page_PSE Fleets/span_Next’))
WebUI.verifyElementText(findTestObject(‘Object Repository/Page_PSE Fleets/p_291,893’), ‘$291,893’)
WebUI.verifyElementText(findTestObject(‘Object Repository/Page_PSE Fleets/p_2’), ‘2’)
WebUI.verifyElementText(findTestObject(‘Object Repository/Page_PSE Fleets/p_30,148’), ‘$30,148’)
WebUI.verifyElementText(findTestObject(‘Object Repository/Page_PSE Fleets/p_60’), ‘$60’)
WebUI.verifyElementText(findTestObject(‘Object Repository/Page_PSE Fleets/p_843 tons’), ‘843 tons’)
WebUI.closeBrowser()
Here is the error
=============== ROOT CAUSE =====================
Caused by: com.kms.katalon.core.webui.exception.WebElementNotFoundException: Web element with id: ‘’ located by ‘//li[@value=‘Medium Or Large Businesses’]’ not found
For trouble shooting, please visit: https://docs.katalon.com/katalon-studio/docs/troubleshooting.html
================================================
04-13-2023 09:59:55 AM click(dropdownItem)
Elapsed time: 1.332s
Unable to click on object ‘’ (Root cause: com.kms.katalon.core.exception.StepFailedException: Unable to click on object ‘’
at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.stepFailed(WebUIKeywordMain.groovy:64)
at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.runKeyword(WebUIKeywordMain.groovy:26)
at com.kms.katalon.core.webui.keyword.builtin.ClickKeyword.click(ClickKeyword.groovy:74)
at com.kms.katalon.core.webui.keyword.builtin.ClickKeyword.execute(ClickKeyword.groovy:40)
at com.kms.katalon.core.keyword.internal.KeywordExecutor.executeKeywordForPlatform(KeywordExecutor.groovy:74)
at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords.click(WebUiBuiltInKeywords.groovy:620)
at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords$click$1.call(Unknown Source)
at New Test Case.run(New Test Case:38)
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.TestCaseExecutor.runScript(TestCaseExecutor.java:448)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:439)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:418)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:410)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:285)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:144)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:135)
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
at TempTestCase1681394368295.run(TempTestCase1681394368295.groovy:25)
Caused by: com.kms.katalon.core.webui.exception.WebElementNotFoundException: Web element with id: ‘’ located by ‘//li[@value=‘Medium Or Large Businesses’]’ not found
at com.kms.katalon.core.webui.common.WebUiCommonHelper.findWebElement(WebUiCommonHelper.java:1376)
at com.kms.katalon.core.webui.keyword.internal.WebUIAbstractKeyword.findWebElement(WebUIAbstractKeyword.groovy:27)
at com.kms.katalon.core.webui.keyword.builtin.ClickKeyword$_click_closure1.doCall(ClickKeyword.groovy:65)
at com.kms.katalon.core.webui.keyword.builtin.ClickKeyword$_click_closure1.call(ClickKeyword.groovy)
at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.runKeyword(WebUIKeywordMain.groovy:20)
at com.kms.katalon.core.webui.keyword.builtin.ClickKeyword.click(ClickKeyword.groovy:74)
at com.kms.katalon.core.webui.keyword.builtin.ClickKeyword.execute(ClickKeyword.groovy:40)
at com.kms.katalon.core.keyword.internal.KeywordExecutor.executeKeywordForPlatform(KeywordExecutor.groovy:74)
at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords.click(WebUiBuiltInKeywords.groovy:620)
at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords$click$1.call(Unknown Source)
at Script1681394125645.run(Script1681394125645.groovy:38)
… 11 more
)
I am trying to select “Medium Or Large Businesses” and the xpath is
/html/body/div[1]/div/div/div/main/form/div[1]/div[1]/div/div[1]/button
I appreciate all your help!
I figured out why it was not working! My value was incorrect. Thanks again!!
One last question, can I create a variable in the variable section and refer it here?
That is correct. If you have the variable stored in the variable section, you can reference that variable.
In Variables Tab
In Script tab
Just note that the variable will be grey, but it will still work.
If you have a variable you would like to use throughout a Test Suite, you can also make use of GlobalVariables
First create the global variable in your profile file
Then call the global variable like so
At the end of the day there is more than one way to skin a cat if you can store the value and call the value you can use it.
awesome! thank you so much! I really appreciate your quick response!
Always a pleasure. Don’t be afraid to mark the correct response as the solution
Will do! I have one last question, I am trying to match 2 strings and for some reason its giving an error.
Caused by: com.kms.katalon.core.exception.StepFailedException: Actual text ‘-$9,589’ and expected text ‘$291,893’ of test object ‘Object Repository/Page_PSE Fleets/p_291,893’ are NOT matched.
URL: EV Calculator
Xpath: /html/body/div[1]/div/main/div[1]/div[1]/div/div[1]/p[2]
Is there an easy way to check if it matches with a variable?
Can you also include the code used for the verifying of the 2 strings?
Because it is getting ‘-$9,589’ but Expecting ‘$291,893’
I actually used the recorder to compare this, If I introduce a delay of 10 seconds, it compares the 2 values, I wanted to find out if there was code that we can write (just like you did for dropdown) rather than using the recorder?
<?xml version="1.0" encoding="UTF-8"?> p_291,893 926621db-a2f4-4cfc-846d-4752214deef7 XPATH //main[@id='main']/div/div/div/div/p[2] CSS p.sc-beqWaB.gwPJoM XPATH true false equals tag Main p 7aa8ec57-0fd7-481d-9440-351561551fad false equals data-design Main app.output.template.summary.iconText.common.text 927b9ae1-230e-4b67-a8af-28b34fbe540e false equals aria-labelledby Main app.output.template.summary.lifetimeSavings.header cf52668a-7073-4545-8127-9b8f5c5c646e false equals aria-level Main 1 86652477-6131-49bc-a8a7-e0554174b6b5 false equals color Main var(--primary-dark-color) 066e5252-53d4-4c05-854f-c335376aee72 false equals role Main heading 777005ab-5b9d-430e-bd11-69573ebb5155 false equals values Main [object Object] bb14e700-5ec6-4b2c-8bfa-a2fc20d3baa4 false equals class Main sc-beqWaB gwPJoM 6c044436-e61f-4656-8ad6-a0f9faed2678 true equals text Main $291,893 665c00a2-1f7d-496a-affc-05db07b8bd44 false equals xpath Main id("main")/div[@class="sc-beqWaB hzgqAi"]/div[@class="sc-beqWaB FJIEc"]/div[@class="sc-beqWaB iHuXXl"]/div[@class="sc-beqWaB kbShFE"]/p[@class="sc-beqWaB gwPJoM"] ccdc2497-a2f0-43b2-9762-5476a1ba2634 true equals xpath:idRelative Main //main[@id='main']/div/div/div/div/p[2] ffa2c943-91ce-424e-b1dd-70f13b480075 false equals xpath:neighbor Main (.//*[normalize-space(text()) and normalize-space(.)='Lifetime Savings'])[1]/following::p[2] 58c863b7-353c-4257-809d-0db13feede99 false equals xpath:neighbor Main (.//*[normalize-space(text()) and normalize-space(.)='Skip to main content'])[1]/following::p[3] 0d6b49f2-b6ed-401f-9208-c45c766bc32d false equals xpath:neighbor Main (.//*[normalize-space(text()) and normalize-space(.)='Vehicle Count'])[1]/preceding::p[1] 673adb25-bdb0-40c5-a050-75f926fa698b false equals xpath:neighbor Main (.//*[normalize-space(text()) and normalize-space(.)='Annual Fuel Savings'])[1]/preceding::p[3] c68c5142-8fdf-4a0c-a190-3021c2c1bb09 false equals xpath:neighbor Main //*/text()[normalize-space(.)='$291,893']/parent::* fddd7e30-cb02-4a3e-9ef2-b9acbfe4d7ca false equals xpath:position Main //p[2] eb7d6ba7-ebbd-439a-bd6a-0e157ab41bc4 false equals xpath:customAttributes Main //p[(text() = '$291,893' or . = '$291,893')] 7d74400b-ebc5-4787-aa81-ab01acb63ba8I make use of Katalon Studio, which also has a recorder build in.
Once I am done recording what I needed, I save the script and then switch over to the script tab in my test case to manually edit the code
Thanks, I did that before as well! I wanted to find out if I can write the code directly in the script to check the value from the xpath to a variable rather than using the recorder?
If I understand correctly, what you can do is run a session first with the URL you want to test
And then make changes in the file and run them directly in the session that is still open
You can then get and compare values as you code
got it! thank you again!
Please use the Code Formatting syntax (enclose lines of codes with a pair of 3 backticks) for better readability for others: