I need help in katalon studio groovy script for if else statement. If the element 'Page_Quick Inbound/input_Bad_quantity' is not found then it should skip the current iteration and continue with the next iteration. 12th line in the code I have tried the if statement but it is not working.
for (def row = 1; row <= findTestData('Ship Plan Data').getRowNumbers(); row++)
{
WebUI.delay(2)
WebUI.setText(findTestObject('Page_Quick Inbound/input_Scan or type SKU_itemId'),
findTestData('Ship Plan Data').getValue('fnsku', row))
rb.keyPress(KeyEvent.VK_ENTER)
WebUI.delay(1)
rb.keyRelease(KeyEvent.VK_ENTER)
WebUI.delay(2)
if (WebUI.verifyElementNotPresent(findTestObject('Page_Quick Inbound/input_Bad_quantity',10,FailureHandling.OPTIONAL) )==true)
{continue}
else{
WebUI.setText(findTestObject('Page_Quick Inbound/input_Bad_quantity'), findTestData('Ship Plan Data').getValue('Quantity',
row))
rb.keyPress(KeyEvent.VK_ENTER)
WebUI.delay(2)
rb.keyRelease(KeyEvent.VK_ENTER)
WebUI.delay(3)
WebUI.setText(findTestObject('Page_Quick Inbound/input_(You can select bin from'), findTestData('Ship Plan Data').getValue(
'bin', row))
rb.keyPress(KeyEvent.VK_ENTER)
WebUI.delay(2)
rb.keyRelease(KeyEvent.VK_ENTER)
WebUI.delay(2)
WebUI.click(findTestObject('Page_Quick Inbound/button_RECEIVE STORE'))
}
}
findTestObject() accepts strings as argument, so the integer and the failure handling need to go.
you have typo in if command:
if (WebUI.verifyElementNotPresent(findTestObject('Page_Quick Inbound/input_Bad_quantity'),10,FailureHandling.OPTIONAL) == true)
finTestObject(),10,FailureHandling
Related
I have to add assertion in 2 test suit out of 3, written below groovy script for adding assertion in all step, but it is considering "Groovy Script" Test Step also, and hence script is getting failed
def testCases = context.testCase.testSuite.getTestCaseList()
testCases.each
{
log.info "~~~Test Case:" + it.name
for(testSteps in it.testStepList)
{
log.info "~~~Test Step:" + testSteps.name
def Assertion5 = testSteps.getAssertionList()
for( a in Assertion5)
{
testSteps.removeAssertion(a)
}
def Assertion6 = testSteps.addAssertion("Contains")
Assertion6.setToken("REST Countries")
}
}
Your help will be appreciated. Thanks in advance.
Of course, groovy scripts steps are considered as testSteps, but testStep contains information about its type that you can filter on.
In your 'for' loop you should add a filter like :
if (testStep.config.type != "groovy")
and then do your processing.
That should work
I have a Jenkinsfile project that requires me to include an 'if statement to ascertain if shell commands within certain methods return an exit code outside of 0.
The first method method 1 works as expected. However i would like to include an if statement to skip the second stage since that shell command in method 2 doesn't exit with 0.
def method1(setup){
sh """
echo ${setup}
"""
}
def method2(setup){
sh """
ech ${setup}
"""
}
node {
stage('print method1'){
method1('paul')
}
// I need an if statement to skip this method since the if statement returns non=zero
stage('This method should be skipped'){
if(method2 returns != 0) //This if condition fails but need something similar to this
method1('paul')
}
}
Any help with this is much appreciated.
You use a default sh step execution in your example which means that exit code is not returned from the command. If exist status is something else than 0 in such case, pipeline fails with the exception. If you want to return exit status and allow the pipeline to continue you have to pass returnStatus: true option, for instance:
int status = sh(script: """
echo ${setup}
""", returnStatus: true)
if (status != 0) {
// do something
}
Source: sh step pipeline documentation
I have the Groovy Script test step named Copying_Bulk_File_To_BulkPayment_Folder.
def rawRequest = context.expand('${SOAP#request}')
def file1 = new File('C:/Actimize/4.23.0.30/Instances/actimize_server_1/QA/BulkPayment/NACHA/Input/RegularNachaPPD.ACH')
file1.write (rawRequest)
After that I have other groovy that should called it 10 times, but it is not doing as expected, below is the respective code.
if( context.loopIndex2 == null )
context.loopIndex2 = 0
if( ++context.loopIndex2 < 10 )
testRunner.gotoStepByName( "Copying_Bulk_File_To_BulkPayment_Folder" )
I did have similar experience. Either gotoStepByName does not seem to work or do not know usage of it correctly.
In order get it to work, please do the following change.
From:
testRunner.gotoStepByName("Copying_Bulk_File_To_BulkPayment_Folder")
To:
testRunner.runTestStepByName('Copying_Bulk_File_To_BulkPayment_Folder')
EDIT: OP mentioned that he still has issue without providing the details. Adding another approach to run the test step.
or try below code instead of gotoStepByName statement.
def step = context.testCase.testSteps['Copying_Bulk_File_To_BulkPayment_Folder']
step.run(testRunner, context)
There are 3 problems in your script.
1) gotoStepByName,
change this to runTestStepByName
2) if( ++context.loopIndex2 < 10 )
Since you want a loop better use a for loop instead of an if condition
for(int i=0; i< 10 ; i++ )
3) File name is same all the time i.e. RegularNachaPPD.ACH , even if you copy 1000 times you will not see this since the file name is same, So you should bring a logic by which every time the name of the file is different
int ok= (new Random().nextInt(100000))
print ok
def FileName='C:/Actimize/4.23.0.30/Instances/actimize_server_1/QA/BulkPayment/NACHA/Input/RegularNachaPPD_' + ok + '.ACH'
Below is the code i used and i was able to create 10 files
TestStepName :- test
if( context.loopIndex2 == null )
context.loopIndex2 = 0
for(int i=0; i< 10 ; i++ )
{
testRunner.runTestStepByName( "Copying_Bulk_File_To_BulkPayment_Folder" )
}
TestStepName :- Copying_Bulk_File_To_BulkPayment_Folder
int ok= (new Random().nextInt(100000))
print ok
def FileName='C:/Actimize/4.23.0.30/Instances/actimize_server_1/QA/BulkPayment/NACHA/Input/RegularNachaPPD_' + ok + '.ACH'
def rawRequest = context.expand('${First Step#request}')
def file1 = new File(FileName)
file1.write (rawRequest)
where SOAP is the name of another test step.
The above logic creates a random number which is appended in the name RegularNachaPPD
Hope it helps. I was able to create 10 files with above code
I am using free version of soapui. In my load test, I want to read request field value from a text file. The file looks like following
0401108937
0401109140
0401109505
0401110330
0401111204
0401111468
0401111589
0401111729
0401111768
In load test, for each request I want to read this file sequentially. I am using the code mentioned in Unique property per SoapUI request using groovy to read the file. How can I use the values from the file in a sequential manner?
I have following test setup script to read the file
def projectDir = context.expand('${projectDir}') + File.separator
def dataFile = "usernames.txt"
try
{
File file = new File(projectDir + dataFile)
context.data = file.readLines()
context.dataCount = context.data.size
log.info " data count" + context.dataCount
context.index = 0; //index to read data array in sequence
}
catch (Exception e)
{
testRunner.fail("Failed to load " + dataFile + " from project directory.")
return
}
In my test, I have following script as test step. I want to read the current index record from array and then increment the index value
def randUserAccount = context.data.get(context.index);
context.setProperty("randUserAccount", randUserAccount)
context.index = ((int)context.index) + 1;
But with this script, I always get 2nd record of the array. The index value is not incrementing.
You defined the variable context.index to 0 and just do +1
You maybe need a loop to read all values.
something like this :
for(int i=0; i <context.data.size; i++){
context.setProperty("randUserAccount", i);
//your code
}
You can add this setup script to the setup script section for load test and access the values in the groovy script test step using:
context.LoadTestContext.index =((int)context.LoadTestContext.index)+1
This might be a late reply but I was facing the same problem for my load testing for some time. Using index as global property solved the issue for me.
Index is set as -1 initially. The below code would increment the index by 1, set the incremented value as global property and then pick the context data for that index.
<confirmationNumber>${=com.eviware.soapui.SoapUI.globalProperties.setPropertyValue( "index", (com.eviware.soapui.SoapUI.globalProperties.getPropertyValue( "index" ).toLong()+1 ).toString()); return (context.data.get( (com.eviware.soapui.SoapUI.globalProperties.getPropertyValue( "index" )).toInteger())) }</confirmationNumber>
I have a table in my app.
Using Capybara and Cucumber, how do I assert that values 4.5 and 1.1 happen only in the Mike's row?
Is such assertion possible in Capybara?
Thanks!
You can use within to scope where you are searching for a specific value:
For example, to assert that value 4.5 happens in the second column of Mike's row, try the following:
within("table tr:nth-child(2)") do
find("td:nth-child(2)").text.should == 4.5
end
You can wrap these in helper methods for ease of use if you would like:
def within_row(num, &block)
within("table tr:nth-child(#{num})", &block)
end
def column_text(num)
find("td:nth-child(#{num})").text
end
Now you could make the same assertion about Mike's row by doing the following:
within_row(2) do
column_text(2).should == 4.1
end
Hopefully you will find one of these techniques useful for what you are trying to do.
Yes, it's possible and easy:
def td_text(n)
find(:xpath, "./td[#{n}]").text
end
h = {2 => 4.5, 3 => 1.1}
all('table tr').each do |row|
within row do
if td_text(1) == 'Mike'
h.each { |i, value| td_text(i).should == value.to_s }
else
h.each { |i, value| td_text(i).should_not == value.to_s }
end
end
end
Here's full script that you can use for testing
Update: I thought about it more. The code above will be very slow as every invocation of find and text in td_text will make new query to browser.
The only way to mitigate it that I see is to use JS and Nokogiri:
source = page.evaluate_script("document.getElementsByTagName('table')[0].innerHTML")
doc = Nokogiri::HTML(source)
def td_text(row, n)
row.xpath("./td[#{n}]").text
end
h = {2 => 4.5, 3 => 1.1}
doc.css('tr').each do |row|
if td_text(row, 1) == 'Mike'
h.each { |i, value| td_text(row, i).should == value.to_s }
else
h.each { |i, value| td_text(row, i).should_not == value.to_s }
end
end
The first variant of code runs about 200 milliseconds at my machine though the second one - 8 milliseconds. Good optimization!