构建状态:
Appium Helper:
网络助手:
参考:帮助者API
CodeCeptJS是一个新的测试框架,用于使用WebDriver(或其他)进行端到端测试。它将浏览器交互作用抽象为从用户的角度编写的简单步骤。一个简单的测试验证网站主页上存在“欢迎”文本的简单测试将看起来像:
Feature ( 'CodeceptJS demo' ) ;
Scenario ( 'check Welcome page on site' , ( { I } ) => {
I . amOnPage ( '/' ) ;
I . see ( 'Welcome' ) ;
} ) ;CodeCeptJS测试是:
I的方法。这使得测试易于阅读,写作和维护,甚至针对非技术人员。 codeceptjs使用助手模块为I对象提供动作。目前,CodeCeptjs有这些帮助者:
还有更多...
CodeCeptJS是CodeCeption的继任者,CodeCeption是PHP流行的全堆栈测试框架。使用CodeCeptJS,您的方案驱动功能和接受测试将尽可能简单清洁。您不必担心Nodejs的异步性质或剧作家,硒,木偶,Testcafe等的各种API,因为CodeCeptjs将它们统一并使其使其同步时起作用。
npm i codeceptjs --save移动到要存储测试(和CodeCeptJs配置)的目录,并执行:
npx codeceptjs init创建和配置测试环境。如果您需要编写Selenium WebDriver测试,建议从助手列表中选择WebDriver。
之后,通过执行来创建您的第一个测试:
npx codeceptjs generate:test现在创建测试,可以执行
npx codeceptjs run如果要使用Typescript编写测试,则仅通过执行来生成标准类型定义:
npx codeceptjs def .稍后,您甚至可以自动更新类型的定义,以包括您自己的自定义帮助者方法。
笔记:
12+或更高版本。 通过示例学习CodeCeptJ。假设我们已经安装了CodeCeptJ并启用了WebDriver Helper。
让我们看看如何处理基本表单测试:
Feature ( 'CodeceptJS Demonstration' ) ;
Scenario ( 'test some forms' , ( { I } ) => {
I . amOnPage ( 'http://simple-form-bootstrap.plataformatec.com.br/documentation' ) ;
I . fillField ( 'Email' , '[email protected]' ) ;
I . fillField ( 'Password' , secret ( '123456' ) ) ;
I . checkOption ( 'Active' ) ;
I . checkOption ( 'Male' ) ;
I . click ( 'Create User' ) ;
I . see ( 'User is valid' ) ;
I . dontSeeInCurrentUrl ( '/documentation' ) ;
} ) ;所有动作均由I对象执行;断言功能从see函数开始。在这些示例中,所有I的方法均来自WebDriver Helper,请参阅参考以了解如何使用它们。
让我们使用run命令执行此测试。附加选项--steps将向我们展示运行过程。我们建议在开发过程中使用--steps或--debug 。
npx codeceptjs run --steps这将产生一个输出:
CodeceptJS Demonstration --
test some forms
• I am on page " http://simple-form-bootstrap.plataformatec.com.br/documentation "
• I fill field " Email " , " [email protected] "
• I fill field " Password " , " **** "
• I check option " Active "
• I check option " Male "
• I click " Create User "
• I see " User is valid "
• I dont see in current url " /documentation "
✓ OK in 17752ms CodeCeptJS具有最终功能,可帮助您开发和调试测试。您可以在任何位置暂停测试执行,并使用交互式壳来尝试不同的操作和定位器。只需在测试中的任何位置添加pause()调用并运行。
可以通过运行以外的测试上下文启动交互式外壳:
npx codeceptjs shell我们用fillField方法填充表格,该方法通过其标签位于表单元素。您可以在测试中通过名称, CSS或XPath定位器找到元素的方式相同:
// by name
I . fillField ( 'user_basic[email]' , '[email protected]' ) ;
// by CSS
I . fillField ( '#user_basic_email' , '[email protected]' ) ;
// don't make us guess locator type, specify it
I . fillField ( { css : '#user_basic_email' } , '[email protected]' ) ;其他方法,例如checkOption ,并以类似的方式click工作。他们可以使用标签,CSS或XPATH定位器来查找要交互的元素。
断言从see或dontSee前缀开始。在我们的情况下,我们声称字符串“用户是有效的”在网页中的某个地方。但是,我们可以通过提供第二个参数将搜索范围缩小到特定元素:
I . see ( 'User is valid' ) ;
// better to specify context:
I . see ( 'User is valid' , '.alert-success' ) ;在这种情况下,只有CSS .alert-success位于位置的内部元素中,才会搜索“用户有效”字符串。
如果您需要从网页返回值并将其直接在测试中使用,则应使用具有grab前缀的方法。预计它们将在async/await功能中使用,它们的结果将在测试中可用:
Feature ( 'CodeceptJS Demonstration' ) ;
Scenario ( 'test page title' , async ( { I } ) => {
I . amOnPage ( 'http://simple-form-bootstrap.plataformatec.com.br/documentation' ) ;
const title = await I . grabTitle ( ) ;
I . expectEqual ( title , 'Example application with SimpleForm and Twitter Bootstrap' ) ; // Avaiable with Expect helper. -> https://codecept.io/helpers/Expect/
} ) ;您可以以相同的方式获取文本,属性或形式值,并在下一测试步骤中使用它们。
可以在用户中登录的通用准备步骤,例如在用户中登录,可以放置在或Background Before :
const { I } = inject ( ) ;
Feature ( 'CodeceptJS Demonstration' ) ;
Before ( ( ) => { // or Background
I . amOnPage ( 'http://simple-form-bootstrap.plataformatec.com.br/documentation' ) ;
} ) ;
Scenario ( 'test some forms' , ( ) => {
I . click ( 'Create User' ) ;
I . see ( 'User is valid' ) ;
I . dontSeeInCurrentUrl ( '/documentation' ) ;
} ) ;
Scenario ( 'test title' , ( ) => {
I . seeInTitle ( 'Example application' ) ;
} ) ; CodeCeptJS提供了在测试中创建和使用页面对象的最简单方法。您可以通过运行创建一个
npx codeceptjs generate pageobject它将为您创建一个页面对象文件,并将其添加到配置中。假设我们创建了一个名为docsPage :
const { I } = inject ( ) ;
module . exports = {
fields : {
email : '#user_basic_email' ,
password : '#user_basic_password'
} ,
submitButton : { css : '#new_user_basic input[type=submit]' } ,
sendForm ( email , password ) {
I . fillField ( this . fields . email , email ) ;
I . fillField ( this . fields . password , password ) ;
I . click ( this . submitButton ) ;
}
}您可以通过在测试参数中提供其名称来轻松注入测试:
Feature ( 'CodeceptJS Demonstration' ) ;
Before ( ( { I } ) => { // or Background
I . amOnPage ( 'http://simple-form-bootstrap.plataformatec.com.br/documentation' ) ;
} ) ;
Scenario ( 'test some forms' , ( { I , docsPage } ) => {
docsPage . sendForm ( '[email protected]' , '123456' ) ;
I . see ( 'User is valid' ) ;
I . dontSeeInCurrentUrl ( '/documentation' ) ;
} ) ;使用TypeScript时,将module.exports替换为自动完成的export 。
Davertmik | Kobenguyent | Vorobeyko | Reubenmiller |
阿赫尔 | apshenkin | 法比奥尔 | Pablopaul |
米拉 | 乔治格里夫 | kmkoushik | Nikocanvacom |
Elukoyanov | 托马索恩 | Gkushang | Tsuemura |
Egorbodnar | 维卡尔普 | Elaichenkov | Borisosipov |
Ngraf | Nitschsb | Hubidu | jploskonka |
Maojunxyz | Abhimanyupandian | 马托莫 | Hatufacci |
MIT©CodeCeptJS团队