自动化测试检测URL的正确性,js获取url并进行校验
难点2个
1.webdriver.io框架怎么获取窗口url信息 getWindowHandles 方法
2.如何校验当前包含有对应的字符串
一般如果有window对象直接
window.location.host window.location.href //需要有window这个对象,否则会报window is not define document.location.href //需要有document这个对象,否则会报document is not define window.open
但是node.js没有window对象,这时候会报window is not define的错误
所以使用 getBrowserWindowHandles 返回的是一个数组表示当前开的窗口,第一个就是[0],第二个就是[1]以此类推
方案1:自动化测试检测网页URL的正确性,对获取的网页URL进行截取,当前的页面是否有对应的域名字符,证明网页截取成功并且正确
if (sharePath == 'facebook'){ shareHref=shareHref.trim().split('.')[1] }else{ shareHref=shareHref.trim().split('/')[2].split('.')[0] } ShareResources.verifyShareFirstResource(shareHref,sharePath)
案例的代码:
1./feature/share-resources.feature
Feature:share resources Background: Given I am login in Resource Library @121392 Scenario: Share resources to facebook When I share first resource to facebook on home page Then The resource is shared to facebook successfully @178949 Scenario: Share resources to twitter When I share first resource to twitter on home page Then The resource is shared to twitter successfully
2./steps/share-resources.js
import {Given, When, Then} from 'cucumber'; import ShareResources from "../../page-objects/resource-library/resources-card-for-share" let shareHref = " " When(/^I share first resource to (\w+) on home page$/, function (sharePath) { shareHref = ShareResources.shareFirstResource(sharePath) }); Then(/^The resource is shared to (\w+) successfully$/, function (sharePath) { if (sharePath == 'facebook'){ shareHref=shareHref.trim().split('.')[1] }else{ shareHref=shareHref.trim().split('/')[2].split('.')[0] } ShareResources.verifyShareFirstResource(shareHref,sharePath) });
3./pageObejct/share-resources.js
import DownloadResources from "./resources-card-for-download"; import assert from "assert" class ResourcesCardForShare{ get resourceListItem(){return $('.ant-list-item')} get itemsList(){return $('#ls-app-list')} get twitterButton(){return $('.menu-item__twitter')} get facebookButton(){return $('.menu-item__facebook')} shareFirstResource(sharePath){ this.itemsList.waitForExist() this.resourceListItem.waitForExist() DownloadResources.operateResourceButton.waitForDisplayed() DownloadResources.operateResourceButton.waitForClickable() DownloadResources.operateResourceButton.click() //avoid getBrowserWindowHandles is none will cause error so have default value first let getBrowserWindowHandles = [] let shareHref = ""; if (sharePath == 'facebook'){ this.facebookButton.click() }else{ this.twitterButton.click() } getBrowserWindowHandles = browser.getWindowHandles()
//这边switchWindow 后面跟的一定要是URL,如果是windowHandles返回的则是窗口id 使用switchWindow则会报错找不到窗口只能哟经switchToWinodows //browser.switchWindow(getBrowserWindowHandles[1]) browser.switchToWindow(getBrowserWindowHandles[1]); shareHref = browser.getUrl().toString() console.log('**************'+shareHref) browser.closeWindow() //should switch to origin window browser.switchToWindow(getBrowserWindowHandles[0]); return shareHref } verifyShareFirstResource(shareHref,sharePath){ assert.strictEqual(shareHref,sharePath) // expect(shareHref).toHaveText(sharePath) } } export default new ResourcesCardForShare()
方案2:expect其实是jest的语法,所以jest的相关方法也可以拿来使用,toHaveText这边只能针对页面元素,如果是字符串 则不能使用
// assert.strictEqual(shareHref,sharePath) expect(shareHref).toMatch(sharePath) // expect(shareHref).toHaveText(sharePath)
声明 欢迎转载,但请保留文章原始出处:) 博客园:https://www.cnblogs.com/chenxiaomeng/
如出现转载未声明 将追究法律责任~谢谢合作