简体   繁体   中英

How to resolve AssertionError in Selenium Python?

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

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

I've just realised that the text has a special character so I cannot convert to float

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

so the value of totalcalamt is

Total Amount: $6.00

therefore I should use

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

This get only the numerical part of the string .

text property returns string even if text looks like a number. "6.00" is not equal to 6.00 . Try

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 =  [float(i) for i in self.driver.find_element_by_xpath("//body[1]/div[1]/div[3]/div[1]/div[1]/div[4]/div[2]/label[1]").text.split() if i.isdigit()][0]
assert float(subTotalAmt) == totalCalAmt == expected_price

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM