Can I store a value from a table search in a variable?

I have this code:

 WebUI.openBrowser('')

 WebUI.navigateToUrl('https://www.texaslending.com/')

 WebUI.click(findTestObject('Page_TexasLending.com - When you think of m_ec7bcf/img_Simplify your search. Select your loan._410ce5'))

WebUI.delay(1)

WebUI.click(findTestObject('Page_Refinance Home  Current Mortgage Rates_4ab5ec/img_Blog_do-not-smush'))

WebUI.delay(1)

WebUI.setText(findTestObject('Page_Sales Funnel - TexasLending.com/input_What is your current loan balance_fie_eccf68'), 
    '653')

WebUI.click(findTestObject('Page_Sales Funnel - TexasLending.com/img_What is your current loan balance_vc_si_5c602b'))

WebUI.delay(1)

WebUI.click(findTestObject('Page_Sales Funnel - TexasLending.com/div_We currently do not loan in this area'))

WebUI.delay(1)

WebUI.setText(findTestObject('Page_Sales Funnel - TexasLending.com/input_Zipcode_zip'), '75065')

WebUI.delay(1)

WebUI.click(findTestObject('Page_Sales Funnel - TexasLending.com/img_We currently do not loan in this area.__d583fb'))

WebUI.delay(1)

WebUI.click(findTestObject('Page_Sales Funnel - TexasLending.com/img_How did you hear about us_vc_single_ima_7c4a04'))

WebUI.setText(findTestObject('Page_Sales Funnel - TexasLending.com/input_First Name_fname'), 'test')

WebUI.setText(findTestObject('Page_Sales Funnel - TexasLending.com/input_Last Name_lname'), 'test')

Here I got a timestamp which inputs different email every time


def address = CustomKeywords.'com.costea.EmailUtil.getMyAddress'() 

WebUI.setText(findTestObject('Page_Sales Funnel - TexasLending.com/input_Email Address_email'), address)

WebUI.setText(findTestObject('Page_Sales Funnel - TexasLending.com/input_Email Address_email'), address)

WebUI.delay(0)

WebUI.click(findTestObject('Page_Sales Funnel - TexasLending.com/input_Phone Number_field27977902'))

WebUI.setText(findTestObject('test2/Page_Sales Funnel - TexasLending.com/input_Phone Number_field27977902'), '1234567890')

WebUI.click(findTestObject('test2/Page_Sales Funnel - TexasLending.com/img_I agree to the Terms and Conditions and_928f25'))

WebUI.closeBrowser()

WebUI.delay(2)

WebUI.openBrowser('')

WebUI.maximizeWindow()

WebUI.navigateToUrl('https://lm.prod.velocify.com/Web/Login.aspx')

WebUI.setText(findTestObject('Object Repository/Page_LeadManager - Login/input_User Name_usernameTextBox'), '')

WebUI.setEncryptedText(findTestObject('Object Repository/Page_LeadManager - Login/input_Password_passwordTextBox'), ')

WebUI.click(findTestObject('Object Repository/Page_LeadManager - Login/input_Password is required._loginButton'))

And from here I want to check the specific email input upper in a table in other page and I am using a table check

String ExpectedValue = address;

WebDriver driver = DriverFactory.getWebDriver()

//Expected value from Table
WebElement Table = driver.findElement(By.xpath('//*[@id="leadtable"]'))

//To locate table
rows_table = Table.findElements(By.tagName('//th/td'))

// To locate rows of the table it will Capture all the rows available in the table
List<WebElement> rows_table = Table.findElements(By.tagName('th'))

// To calculate no of rows In table
int rows_count = rows_table.size()

// Loop will execute for all the rows of the table
Loop: 
for (int row = 0; row < rows_count; row++) {
    List<WebElement> Columns_row = rows_table.get(row).findElements(By.tagName('a'))

    int columns_count = Columns_row.size()

    println((('Number of cells In Row ' + row) + ' are ') + columns_count)

for (int column = 0; column < columns_count; column++) {
        String celltext = Columns_row.get(column).getText()

        
        println((((('Cell Value Of row number ' + row) + ' and column number ') + column) + ' Is ') + celltext)

        if (celltext == ExpectedValue) {
            println('Lead is present ' + Columns_row.get(2).getText())
        } else {
            println('Lead is not added')

            break
        }
    }
}

Then here I want to add the returned value from this loop in a variable to use this code, instead of def v = WebUI. I want to use dev = returned values from table

def v = WebUI.getAttribute(findTestObject('Page_Sales Funnel - TexasLending.com/input_Email Address_email'),
        'value')
def result = WebUI.verifyEqual(v, address)
if (result) {
	WebUI.comment("Good! The address ${address} is what I expected")	
} else {
	WebUI.comment("Nein, die Adresse ${address} ist nicht das, was ich erwartet habe")
}

Thank you in advanced!

Sorry, I donā€™t see where youā€™re getting ā€œreturned values from tableā€.

In essence what you want to do is

def my-variable = "whatever value you want"

If you need to use the variable in another test case (while executing a suite), you should create a GlobalVariable in your profile, thenā€¦

GlobalVariable.my-variable = "whatever value you want"

No, I want in the same test case and def my-variable = should be added from the table check. I am not very clear? Should I provide more details?

Probably not. I just donā€™t see WHERE in your code you want to add the variable. Perhaps take a screen shot of your code and place an arrow to show me? Or write it again with NEW VARIABLE HEREā€¦

You see, loops donā€™t RETURN a value.

for (int column = 0; column < columns_count; column++) {
        String celltext = Columns_row.get(column).getText()

        
        println((((('Cell Value Of row number ' + row) + ' and column number ') + column) + ' Is ') + celltext)

        if (celltext == ExpectedValue) {
            println('Lead is present ' + Columns_row.get(2).getText())
        } else {
            println('Lead is not added')

            break
        }
    }
}
New VARIABLE HERE

and then I want to use:

def v = NEW VARIABLE 
def result = WebUI.verifyEqual(v, address)
if (result) {
	WebUI.comment("Good! The address ${address} is what I expected")	
} else {
	WebUI.comment("Nein, die Adresse ${address} ist nicht das, was ich erwartet habe")
}


So I cannot check the table to see if the specific email added from keyword and then to verify ?

Nobody said you canā€™t do it. I just donā€™t understand what value you want to capture into your new variable.

Is it this value?

image

Yes, this code is taken from Documentation and I tried to adapt to my test case. and the value which I want to return should be the value inputted with the keyword.
From this column should take the value from keyword

The keyword is used on this line of code:

def address = CustomKeywords.'com.costea.EmailUtil.getMyAddress'() 

WebUI.setText(findTestObject('Page_Sales Funnel - TexasLending.com/input_Email Address_email'), address)

And this email is sent to a DB( Image) and there I am looking to see if this email exist in that page. And that is the reason why I use the table check.

Sorry, I canā€™t understand what youā€™re asking of me.

@Russ_Thomas

So I am using a specific keyword to input an email in a contact form with a timestamp and then I go to a DB page and I want to check if that email is returned in that DB page(Image upload early). and I want to get a text from a Web table and verify it. And this ā€œemailā€ from DB to add in a variable and check with another variable to see if are equal like here on this code:

def v = NEW VARIABLE 
def result = WebUI.verifyEqual(v, address)
if (result) {
	WebUI.comment("Good! The address ${address} is what I expected")	
} else {
	WebUI.comment("Nein, die Adresse ${address} ist nicht das, was ich erwartet habe")
}

Or another explanation On page A I used an email with a timestamp and on-page B I want to check that specific email but page B is a table page.

Answer my question from this post - DONā€™T say anything else, just answer the question.

Yes, that will be the value.

But that loop then carries on, searching AGAIN - I think you need a break in that IF block (like you have in the else block).

I think you copied this code from somewhere and you are trying to hammer it into you code. It looks very wrong for what I think you trying to achieve.

@Russ_Thomas
Yes I copied from :


and try on my own.

Okay, so if thatā€™s the value, then I THINK I know what you want. But first, what do you want the code to do if you DONā€™T find the value. Iā€™m asking about this part:

image

I want to fail because if is fail I know something is wrong with the first page.

Try this:

// Put this in your imports up top
import com.kms.katalon.core.exception.StepFailedException as StepFailedException

String celltext
boolean found = false
for (int column = 0; column < columns_count; column++) {
  celltext = Columns_row.get(column).getText()

  println((((('Cell Value Of row number ' + row) + ' and column number ') + column) + ' Is ') + celltext)

  if (celltext == ExpectedValue) {
    found = true
    println('Lead is present ' + Columns_row.get(2).getText())
    break;
  }
}

if(!found) {
  throw new StepFailedException("Failed to find the email")
}

//New VARIABLE HERE
// celltext contains the value - don't need a new variable
// You might want to change the name to something better than celltext

Itā€™s failed and I am thinking my table row where the value is assigned is changed every time. Why I am saying that because If I used Columns_row.get(2) will fail but the email is there and I was thinking and compare to documentation and I was doing like that I input in the search field to search the specific email and on Columns_row.get I added (1) and was fail again, then I tried with (0) same issue then I was trying with the specific row from the page and again fail, the code works how I want on the fail one but on the pass no:(.
Here is the image:

@costea.georgian89

You have messed up your problem. It is very difficult for others to get into it. You should divide your problem into pieces of smaller/manageable size, and then ask others for help.

Let me make a proposal for you.

  1. You have a URL ā€œLeadManager - Loginā€. You know the credentials to log into it. You can navigate to a Web Page where you find a Table like this:
  2. In the table we can find a row with Email="abchang@hotmail.com" , ID=ā€œ598042ā€
  3. In the table we do not find a row with Email="foobar@gmail.com"

So, @costea.georgian89, could you create a new project. You want to make a test case so that

  • the script searches the table for a row of Email="abchang@hotmail.com" and print its ID value(ā€˜598042ā€™)
  • also the script should be able to find that a row of Email="foobar@gmail.com" is not present in the table, print message ā€œfoobar@gmail.com is not our customerā€.
  • the script should be able to deal with many other Email addresses as many as you want.

In this practice, I hope you to learn how to modularise your code by introducing methods inside a Test Case and reuse it so that you can process multiple Email addresses.

If you are to make a post about this exercise, please make a ZIP file of the project and share it.