0

I am currently working on a project in Selenium in Python and I am getting AssertionError when I put a specific parametrised value 6.00 in the parentheses.

Test.py

Class Test:

def test_voidnewcard(self):  
    np.createReqNewCard(expected_price= 6.00)

NewCard.py

Class NewCard:

def createReqNewCard(self, expected_price):
        self.driver.find_element_by_xpath(self.jobOrder_xpath).send_keys(self.jobReqNewCard)
        self.driver.find_element_by_xpath(self.excelButton_xpath).send_keys(self.excpath)
        self.driver.find_element_by_xpath(self.topup_textbox_xpath).send_keys("1")
        self.driver.find_element_by_xpath(self.calculateAmount_xpath).click()
        subTotalAmt =  self.driver.find_element_by_xpath("//body[1]/div[1]/div[3]/div[1]/div[1]/table[1]/tbody[1]/tr[1]/td[4]/div[1]").text
        totalCalAmt =  self.driver.find_element_by_xpath("//body[1]/div[1]/div[3]/div[1]/div[1]/div[4]/div[2]/label[1]").text.replace("Total amount: $", "")
        assert subTotalAmt == totalCalAmt == expected_price, "Price mismatch! Expected price is %s, subtotal is %s, total price is %s" % (round(expected_price,2), subTotalAmt, totalCalAmt)
        self.driver.find_element_by_xpath(self.buttonSubmit_xpath).click()
        time.sleep(8)

Error StackTrace(If I put 6.00 as the parametrised value

Failure
Traceback (most recent call last):
  File "C:\Python27\lib\unittest\case.py", line 329, in run
    testMethod()
  File "C:\Users\lukegoh\Desktop\Python Projects\SoftwareAutomationTesting\testCases\AutoTest.py", line 77, in test_voidnewcard
    np.createReqNewCard(expected_price= 6.00)
  File "C:\Users\lukegoh\Desktop\Python Projects\SoftwareAutomationTesting\newCard\NewCard.py", line 29, in createReqNewCard
    assert subTotalAmt == totalCalAmt == expected_price, "Price mismatch! Expected price is %s, subtotal is %s, total price is %s" % (round(expected_price,2), subTotalAmt, totalCalAmt)
AssertionError: Price mismatch! Expected price is 6.0, subtotal is 6.00, total price is Total Amount: $6.00

Error StackTrace(If I put "6.00" as the parametrised value)

Traceback (most recent call last):
  File "C:\Python27\lib\unittest\case.py", line 329, in run
    testMethod()
  File "C:\Users\lukegoh\Desktop\Python Projects\SoftwareAutomationTesting\testCases\AutoTest.py", line 77, in test_voidnewcard
    np.createReqNewCard(expected_price='6.00')
  File "C:\Users\lukegoh\Desktop\Python Projects\SoftwareAutomationTesting\newCard\NewCard.py", line 29, in createReqNewCard
    assert subTotalAmt == totalCalAmt == expected_price, "Price mismatch! Expected price is %s, subtotal is %s, total price is %s" % (expected_price, subTotalAmt, totalCalAmt)
AssertionError: Price mismatch! Expected price is 6.00, subtotal is 6.00, total price is Total Amount: $6.00

And if I parse subTotalAmt and calculateTotalAmt into float like here,

NewCard.py

   subTotalAmt =  self.driver.find_element_by_xpath("//body[1]/div[1]/div[3]/div[1]/div[1]/table[1]/tbody[1]/tr[1]/td[4]/div[1]").text
        totalCalAmt =  self.driver.find_element_by_xpath("//label[contains(text(),'Total Amount: $6.00')]").text.replace('Total amount: $6.00', '')
        assert float(subTotalAmt) == float(totalCalAmt) == expected_price, "Price mismatch! Expected price is %s, subtotal is %s, total price is %s" % (expected_price, subTotalAmt, totalCalAmt)

I will get this,

Error StackTrace

Traceback (most recent call last):
  File "C:\Python27\lib\unittest\case.py", line 329, in run
    testMethod()
  File "C:\Users\lukegoh\Desktop\Python Projects\SoftwareAutomationTesting\testCases\AutoTest.py", line 77, in test_voidnewcard
    np.createReqNewCard(expected_price=6.00)
  File "C:\Users\lukegoh\Desktop\Python Projects\SoftwareAutomationTesting\newCard\NewCard.py", line 29, in createReqNewCard
    assert float(subTotalAmt) == float(totalCalAmt) == expected_price, "Price mismatch! Expected price is %s, subtotal is %s, total price is %s" % (expected_price, subTotalAmt, totalCalAmt)
ValueError: could not convert string to float: Total Amount: $6.00

Can someone solve this issue? I just can't seem to diagnose this problem

1
  • Can somebody solve this problem?
    – TechGeek49
    Feb 4 at 7:32
1

your text has a special character so you cannot convert to float

see the error

ValueError: could not convert string to float: Total Amount: $6.00

so the value of subtotalamt is

Total Amount: $6.00

use

float(subTotalAmt.split('$')[1])

this get only the number part

Note: This will check only the amount

2
  • actually it's not subTotalAmt. It should be calculateTotalAmt because it returns Total Amount: $6.00 everytime I get error
    – TechGeek49
    Feb 4 at 10:13
  • 1
    Thanks brother! Appreciate your help
    – TechGeek49
    Feb 4 at 10:16
0

totalCalAmt and subTotalAmt are strings. Comparing strings with numbers will rely on Python's choice in type casting.

I would suggest parsing these two strings into numbers and then comparing them with expected_price.

2
  • You mean casting isit?
    – TechGeek49
    Feb 4 at 8:43
  • Yes, you can simply cast it, after you clean up any non-numerical character. Feb 4 at 22:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.