1 - Catch all the elements that matches the following xpath : "//div[@class='price']/div[@class='pricenew'] | //div[@class='price']/div[@class='priceold'] | //div[@class='price']/div[@class='oneprice']"
2 - With that done and stored in an ArrayList variable , I wanna iterate through it and perform a check on each item to see if they have the correct symbol . For that , I have created a Keyword
@Keyword
def checkElements(ArrayList<WebElement> list, String currency) {
for (WebElement price : list) {
String priceItem = WebUI.getText(list(price))
if (WebUI.verifyElementText(priceItem, currency)) {
continue
} else {
System.println("This item doesn't have the correct currency symbol")
}
}
}
The thing is : I have tried using findElements , I have tried using findTestObject , but none of them works (It doesnt get the elements and store in an array)
Caused by: groovy.lang.MissingMethodException: No signature of method: java.util.ArrayList.call() is applicable for argument types: (org.openqa.selenium.support.events.EventFiringWebDriver$EventFiringWebElement) values: [[[CChromeDriver: chrome on WINDOWS (28974eee30b54427ffbfcfa46388ac70)] -> xpath: //div[@class='price']/div[@class='pricenew'] | //div[@class='price']/div[@class='priceold'] | //div[@class='price']/div[@class='oneprice']]]
What happens when you just try to combine the divs by the class instead of the union?
Also, your code would be easier to review if you put 3 backticks (like ``` ) on a row by themselves above your code and 3 backticks on a row below your code. The backtick is found on the same key as the tilde ( ~ ) in the upper left of the keyboard.
Maybe like:
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import org.openqa.selenium.By as By
import org.openqa.selenium.WebDriver as WebDriver
import org.openqa.selenium.WebElement as WebElement
import com.kms.katalon.core.webui.driver.DriverFactory as DriverFactory
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
WebUI.openBrowser(‘https://automationteststore.com/’)
WebUI.maximizeWindow()
WebDriver driver = DriverFactory.getWebDriver()
List<WebElement> listPrices = driver.findElements(By.xpath("//div[@class='price']/div[@class='pricenew' or @class='priceold' or @class='oneprice']"))
CustomKeywords.'checkElement.checkElements'(listPrices, '$')
WebUI.enhancedClick(findTestObject('Home_Page_Elements/dropdownToggle'))
WebUI.enhancedClick(findTestObject('Home_Page_Elements/dropdownPound'))
CustomKeywords.'checkElement.checkElements'(listPrices, '£')
WebUI.enhancedClick(findTestObject('Home_Page_Elements/dropdownEuro'))
CustomKeywords.'checkElement.checkElements'(listPrices, '€')
WebUI.enhancedClick(findTestObject('Home_Page_Elements/dropdownDollar'))
CustomKeywords.'checkElement.checkElements'(listPrices, '$')
I actually added the backticks but somehow it didnt work as intended.
I tried your “way” and its giving the following error :
Caused by: groovy.lang.MissingMethodException: No signature of method: java.util.ArrayList.call() is applicable for argument types: (org.openqa.selenium.support.events.EventFiringWebDriver$EventFiringWebElement) values: [[[CChromeDriver: chrome on WINDOWS (d7da09f8894c95c449c2a8593278a7fb)] -> xpath: //div[@class='price']/div[@class='pricenew' or @class='priceold' or @class='oneprice']]]
Do you have any idea on how can I do it? I have tried like one “big” string but it fails , I can’t understand what the <br> tag does with the selector at this point.
Explaining better : This is a test to register a user, I just wanna assert that if the person tries to create the account without filling mandatory fields it’s going to throw this error on the redbox, and then I want to check if the messages that appear there are correct to the fields that were not filled.
How about using verifyTextPresent for each of the messages or as many as needed? Lastly, I take a screen shot of the page to see if any of the messages get changed between revisions, such as punctuation being removed or altered.
WebUI.verifyTextPresent("Login name must be alphanumeric only...", false)
WebUI.verifyTextPresent("First Name must be between...", false)
WebUI.takeScreenshot(gReportPathway + "LoginPage9.png")
There is also another way to check if the text in the Alert box at the top matches the required fields that has not been filled in.
Unfortunately because the Alert box text does not match the order of the input fields, I had to hard code the order in which it checks the input field alert text.
You can add this method to the very bottom of your test case or create a custom keyword for it
def checkRequiredFields() {
//Set order of alert box text to the order of the required fields
Integer[] rows = [12, 0, 1, 2, 6, 8, 10, 9, 13, 14]
//Get the text from the Alert Box
String actualAlert = WebUI.executeJavaScript('return document.querySelectorAll(".alert.alert-error.alert-danger")[0].innerText', []).substring(2).replaceAll('\n', ' ')
//Create List to store required field text
List alerts = []
//Iterate over required fields and add the required field text to the list
rows.each {it ->
try {
String inputAlert = WebUI.executeJavaScript('return document.querySelectorAll("div.form-group")[' + it.toString() + '].querySelectorAll("span.help-block")[0].innerText', [])
if(inputAlert != '') {
alerts.add(inputAlert)
}
}catch(Exception ex) {
//Catch expection
}
}
//Join the List into a single string
String expectedAlert = alerts.join(' ')
//Check if expected string matches the alert box text
WebUI.verifyMatch(actualAlert, expectedAlert, false)
}
Then each time you want to check if the alert text from the fields match the alert text in the alert box, you just have to call the method:
checkRequiredFields()
So by using that method it will check that the alert box text is correct no matter what required fields were filled in or not according to the alert text of each field that was left out.
I then take that string and remove the x in the start and replace all the \n characters with a blank space.
The \n character is a line break, which is also those <br> tags you see
Here is the output of the original string in devtools: