AttributeError:' str'对象没有属性' __ name __'回

时间:2017-06-15 13:56:27

标签: python csv sst

我希望只是通过CSV导入信息列表并将其转换为简单的操作,但是我收到以下错误消息:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sst/cases.py", line 217, in run_test_script
    exec(self.code, self.context)
  File "./randd/exec_tests.py", line 39, in <module>
    use_csv("randd/testcases/login.csv")
  File "./randd/exec_tests.py", line 31, in use_csv
    wait_for(action_input, locator_from_xpath(str(locator_input)))
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sst/actions.py", line 146, in inner
    return func(*args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sst/actions.py", line 926, in wait_for
    return _wait_for(condition, False, _TIMEOUT, _POLL, *args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sst/actions.py", line 885, in _wait_for
    if config._current_test_assertion_count is not None and 'assert' in condition.__name__:
AttributeError: 'str' object has no attribute '__name__'

以下是使用的Python代码:

# Import Libs
import logging
import csv
import sys
import testdata
from sst.actions import (run_test, go_to, click_element, assert_displayed, wait_for, write_textfield, sleep)
from sst.locators import (find_within, locator_from_css_selector, locator_from_xpath)

logger = logging.getLogger("CSVTests")

def use_csv(csv_file_path):

    #file = open(sys.argv[1], 'rb')
    file = open(csv_file_path, 'rb')
    testcase = csv.reader(file)
    next(testcase)

    for row in testcase:

        testcase_id = row[0]
        action_input = row[1]
        locator_input = row[2]
        detail = row[3]
        description = row[4]

        logger.debug(str(testcase_id))
        print(action_input)
        print(locator_input)

        # print(wait_for(action_input, locator_from_xpath(locator_input)))
        wait_for(action_input, locator_from_xpath(str(locator_input)))

    file.close()

# Goto Build
go_to(testdata.get_base_url())

# Run Tests
use_csv("randd/testcases/login.csv")

请参阅下面的CSV代码段:

TestCase ID,Action,Element / Locator,Option/Text/Result,Description
1,assert_displayed,"//body//div[@class='container-fluid']//div[contains(@class,'box')]",n/a,Checking that the login box is displayed.
2,assert_displayed,"//body//div[@class='container-fluid']//div[contains(@class,'box')]/div[@class='icon-holder']",n/a,Checking that the Frog icon is displayed.

1 个答案:

答案 0 :(得分:0)

发生错误是因为在sst库代码中可以访问__name__

condition.__name__

condition不应该是字符串对象,但收到的当前值实际上是一个字符串。

我的猜测是在你的代码中,在行中:

wait_for(action_input, locator_from_xpath(str(locator_input)))
你没有&#39;必须将locator_input包裹到str()中,所以:

wait_for(action_input, locator_from_xpath(locator_input))

应该有效