【持续集成和交付】项目环境配置:在Jenkins中运行项目

前言

一直想学习自动化测试,但是都没行动,业余时间学习零零碎碎并记录20210424。

11、持续集成和交付

  • Jenkins环境搭建
  • 项目环境配置
  • 邮件通知
  • 定时项目执行

在Jenkins中运行项目

实操

1、终端运行,之前写好的用户登录代码

Last login: Sat Apr 24 10:38:43 on ttys000
 ~ % cd /Users/ff/PycharmProjects_py3/Selenium_project/testcases/ddt
ddt % pytest -sv test_user_login.py
=============================================================================== test session starts ===============================================================================
platform darwin -- Python 3.7.3, pytest-6.2.3, py-1.10.0, pluggy-0.13.1 -- /Library/Developer/CommandLineTools/usr/bin/python3
cachedir: .pytest_cache
rootdir: /Users/ff/PycharmProjects_py3/Selenium_project/testcases/ddt
plugins: dependency-0.5.1, allure-pytest-2.8.40
collected 2 items

test_user_login.py::TestUserLogin::test_user_login_ok[-admin-\u8d26\u53f7\u4e0d\u80fd\u4e3a\u7a7a] FAILED
test_user_login.py::TestUserLogin::test_user_login_ok[admin-admin-\u7528\u6237\u4e2d\u5fc3] PASSED

==================================================================================== FAILURES =====================================================================================
__________________________________________________ TestUserLogin.test_user_login_ok[-admin-\u8d26\u53f7\u4e0d\u80fd\u4e3a\u7a7a] __________________________________________________

self = <testcases.ddt.test_user_login.TestUserLogin object at 0x105c3f710>, user = '', pwd = 'admin', expected = '账号不能为空'

    @pytest.mark.parametrize('user,pwd,expected',login_data)
    def test_user_login_ok(self,user,pwd,expected):
        # 输入用户名
        self.driver.find_element_by_name('user').clear()    # 为了把上面案例输入的值清空
        self.driver.find_element_by_name('user').send_keys(user)
        # 输入密码
        self.driver.find_element_by_name('pwd').clear()
        self.driver.find_element_by_name('pwd').send_keys(pwd)
        # 点击登录
        # self.driver.find_element_by_xpath('/html/body/div/div/div/form/div[3]/div/button').click()
        self.driver.find_element_by_css_selector('body > div > div > div > form > div.row > div > button').click()


        # 等待标题
        if user =='':
>           WebDriverWait(self.driver, 5).until(EC.title_is(expected))

test_user_login.py:39:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
/Library/Python/3.7/site-packages/selenium/webdriver/support/wait.py:71: in until
    value = method(self._driver)
/Library/Python/3.7/site-packages/selenium/webdriver/support/expected_conditions.py:39: in __call__
    return self.title == driver.title
/Library/Python/3.7/site-packages/selenium/webdriver/remote/webdriver.py:342: in title
    resp = self.execute(Command.GET_TITLE)
/Library/Python/3.7/site-packages/selenium/webdriver/remote/webdriver.py:321: in execute
    self.error_handler.check_response(response)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <selenium.webdriver.remote.errorhandler.ErrorHandler object at 0x105c6b208>
response = {'status': 500, 'value': '{"value":{"data":{"text":"账号不能为空"},"error":"unexpected alert open","message":"unexpected ale...fff69860109 _pthread_start + 148\\n17  libsystem_pthread.dylib             0x00007fff6985bb8b thread_start + 15\\n"}}'}

    def check_response(self, response):
        """
        Checks that a JSON response from the WebDriver does not have an error.

        :Args:
         - response - The JSON response from the WebDriver server as a dictionary
           object.

        :Raises: If the response contains an error message.
        """
        status = response.get('status', None)
        if status is None or status == ErrorCode.SUCCESS:
            return
        value = None
        message = response.get("message", "")
        screen = response.get("screen", "")
        stacktrace = None
        if isinstance(status, int):
            value_json = response.get('value', None)
            if value_json and isinstance(value_json, basestring):
                import json
                try:
                    value = json.loads(value_json)
                    if len(value.keys()) == 1:
                        value = value['value']
                    status = value.get('error', None)
                    if status is None:
                        status = value["status"]
                        message = value["value"]
                        if not isinstance(message, basestring):
                            value = message
                            message = message.get('message')
                    else:
                        message = value.get('message', None)
                except ValueError:
                    pass

        exception_class = ErrorInResponseException
        if status in ErrorCode.NO_SUCH_ELEMENT:
            exception_class = NoSuchElementException
        elif status in ErrorCode.NO_SUCH_FRAME:
            exception_class = NoSuchFrameException
        elif status in ErrorCode.NO_SUCH_WINDOW:
            exception_class = NoSuchWindowException
        elif status in ErrorCode.STALE_ELEMENT_REFERENCE:
            exception_class = StaleElementReferenceException
        elif status in ErrorCode.ELEMENT_NOT_VISIBLE:
            exception_class = ElementNotVisibleException
        elif status in ErrorCode.INVALID_ELEMENT_STATE:
            exception_class = InvalidElementStateException
        elif status in ErrorCode.INVALID_SELECTOR \
                or status in ErrorCode.INVALID_XPATH_SELECTOR \
                or status in ErrorCode.INVALID_XPATH_SELECTOR_RETURN_TYPER:
            exception_class = InvalidSelectorException
        elif status in ErrorCode.ELEMENT_IS_NOT_SELECTABLE:
            exception_class = ElementNotSelectableException
        elif status in ErrorCode.ELEMENT_NOT_INTERACTABLE:
            exception_class = ElementNotInteractableException
        elif status in ErrorCode.INVALID_COOKIE_DOMAIN:
            exception_class = InvalidCookieDomainException
        elif status in ErrorCode.UNABLE_TO_SET_COOKIE:
            exception_class = UnableToSetCookieException
        elif status in ErrorCode.TIMEOUT:
            exception_class = TimeoutException
        elif status in ErrorCode.SCRIPT_TIMEOUT:
            exception_class = TimeoutException
        elif status in ErrorCode.UNKNOWN_ERROR:
            exception_class = WebDriverException
        elif status in ErrorCode.UNEXPECTED_ALERT_OPEN:
            exception_class = UnexpectedAlertPresentException
        elif status in ErrorCode.NO_ALERT_OPEN:
            exception_class = NoAlertPresentException
        elif status in ErrorCode.IME_NOT_AVAILABLE:
            exception_class = ImeNotAvailableException
        elif status in ErrorCode.IME_ENGINE_ACTIVATION_FAILED:
            exception_class = ImeActivationFailedException
        elif status in ErrorCode.MOVE_TARGET_OUT_OF_BOUNDS:
            exception_class = MoveTargetOutOfBoundsException
        elif status in ErrorCode.JAVASCRIPT_ERROR:
            exception_class = JavascriptException
        elif status in ErrorCode.SESSION_NOT_CREATED:
            exception_class = SessionNotCreatedException
        elif status in ErrorCode.INVALID_ARGUMENT:
            exception_class = InvalidArgumentException
        elif status in ErrorCode.NO_SUCH_COOKIE:
            exception_class = NoSuchCookieException
        elif status in ErrorCode.UNABLE_TO_CAPTURE_SCREEN:
            exception_class = ScreenshotException
        elif status in ErrorCode.ELEMENT_CLICK_INTERCEPTED:
            exception_class = ElementClickInterceptedException
        elif status in ErrorCode.INSECURE_CERTIFICATE:
            exception_class = InsecureCertificateException
        elif status in ErrorCode.INVALID_COORDINATES:
            exception_class = InvalidCoordinatesException
        elif status in ErrorCode.INVALID_SESSION_ID:
            exception_class = InvalidSessionIdException
        elif status in ErrorCode.UNKNOWN_METHOD:
            exception_class = UnknownMethodException
        else:
            exception_class = WebDriverException
        if value == '' or value is None:
            value = response['value']
        if isinstance(value, basestring):
            if exception_class == ErrorInResponseException:
                raise exception_class(response, value)
            raise exception_class(value)
        if message == "" and 'message' in value:
            message = value['message']

        screen = None
        if 'screen' in value:
            screen = value['screen']

        stacktrace = None
        if 'stackTrace' in value and value['stackTrace']:
            stacktrace = []
            try:
                for frame in value['stackTrace']:
                    line = self._value_or_default(frame, 'lineNumber', '')
                    file = self._value_or_default(frame, 'fileName', '<anonymous>')
                    if line:
                        file = "%s:%s" % (file, line)
                    meth = self._value_or_default(frame, 'methodName', '<anonymous>')
                    if 'className' in frame:
                        meth = "%s.%s" % (frame['className'], meth)
                    msg = "    at %s (%s)"
                    msg = msg % (meth, file)
                    stacktrace.append(msg)
            except TypeError:
                pass
        if exception_class == ErrorInResponseException:
            raise exception_class(response, message)
        elif exception_class == UnexpectedAlertPresentException:
            alert_text = None
            if 'data' in value:
                alert_text = value['data'].get('text')
            elif 'alert' in value:
                alert_text = value['alert'].get('text')
>           raise exception_class(message, screen, stacktrace, alert_text)
E           selenium.common.exceptions.UnexpectedAlertPresentException: Alert Text: 账号不能为空
E           Message: unexpected alert open: {Alert text : 账号不能为空}
E             (Session info: chrome=90.0.4430.72)

/Library/Python/3.7/site-packages/selenium/webdriver/remote/errorhandler.py:241: UnexpectedAlertPresentException
============================================================================= short test summary info =============================================================================
FAILED test_user_login.py::TestUserLogin::test_user_login_ok[-admin-\u8d26\u53f7\u4e0d\u80fd\u4e3a\u7a7a] - selenium.common.exceptions.UnexpectedAlertPresentException: Alert Te...
=========================================================================== 1 failed, 1 passed in 7.15s ===========================================================================
 ddt %

2、Jenkins运行。到对应下载路径,用java -jar jenkins.war --httpPort=8081的方式启动,然后配置

输入任务名称,如:myproject,点击确定

再拖到最底部

配置终端执行脚本然后保存

点击Build Now运行

第一次构建我用ddt的用户登录,不知道为啥变红了,但是实际是可以的,验证登录失败、成功。

然后第二次就选择pytest里的用户登录,跳过登录失败的方法,就没变红了

这样就完成简单的运行了

“永不放弃,总有希望在前面等待!”送给自己,也送给正在阅读文章的博友们~

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小慌慌

感谢博友的鼓励,快乐分享~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值