Skip to main content
 首页 » 编程设计

python之在 Sauce Labs 中使用 Python 设置通过/失败以进行自动化测试

2024年11月01日7mengfanrong

所以,对于这种事情,我完全是个菜鸟,我需要一些帮助。我在一家电子商务公司从事软件 QA 工作,我们开始使用 Saucelabs 进行自动化测试。我正在学习 python,但目前几乎一无所知。我可以在 Selenium IDE 中构建一个像样的测试,在 Python/Selenium Webdriver 中导出,然后运行测试。没什么大不了。但是,如何在界面上设置通过/失败标志?我们的一位开发人员编写了一个并行脚本,因此我可以一次运行大量测试,但为了做到这一点,我需要能够一眼看出哪些测试通过了哪些测试失败了。你能帮助我吗?谢谢!

此外,您了解的有关 Selenium Webdriver 的任何教程也会有所帮助!真的很想学这些东西!

请您参考如下方法:

我是这样做的,首先你需要导入一些东西

# These next imports for reporting Test status to Sauce Labs 
import sys 
import httplib 
import base64 
 
try: 
    import json 
except ImportError: 
    import simplejson as json 

那么你需要这个配置

#Config to connect to SauceLabs REST API 
config = {"username": "yourusernamehere", 
          "access-key": "youraccesskeyhere"} 

然后你进行测试。最后,在您的 TearDown 之前,您需要包含

# Curl call to SauceLabs API to report Job Result 
    def set_test_status(self, jobid, passed): 
        base64string = base64.encodestring('%s:%s' % (config['username'], config['access-key']))[:-1] 
        body_content = json.dumps({"passed": passed}) 
        connection = httplib.HTTPConnection("saucelabs.com") 
        connection.request('PUT', '/rest/v1/%s/jobs/%s' % (config['username'], jobid), 
                       body_content, 
                       headers={"Authorization": "Basic %s" % base64string}) 
        result = connection.getresponse() 
        return result.status == 200 

然后在您的拆解中,您需要包含某种if 逻辑。我是这样做的(而且有效)

def tearDown(self): 
    # sys.exc_info should be (None, None, None) if everything is OK, it fills with some values if something went wrong 
    # This if reports to Sauce Labs the outcome of the Test where True = Pass and False = Failed 
    if sys.exc_info() == (None, None, None): 
        self.set_test_status(self.driver.session_id, True) 
    else: 
        self.set_test_status(self.driver.session_id, False) 
    self.driver.quit() 
    self.assertEqual([], self.verificationErrors) 

这对我有用