避免在登录表单中使用 WebDriverIO 重新输入 user/email 和密码
Avoid re-entering user/email and password with WebDriverIO in login form
我想优化我的 WebdriverIO 测试。当我 运行 使用 WebdriverIO 的测试套件时,我试图避免重新输入用户名和密码。 (Chromedriver)
以下两个文件是一个模块的一部分,一共有4个模块。
第一个功能文件:
var name = 'Andrea' + Math.floor((Math.random() * 1000000) + 1);
var ssn = 'V-' + Math.floor((Math.random() * 1000000) + 1);
var url = 'http://someurl.com';
var new_contact = 'https://someurl.com/client/add';
describe('Some contact is create', function() {
it('Should login to the system', function() {
browser.url(url)
browser.setValue('#email','xxxxxxxx@xxxx.com')
browser.setValue('#password','xxxxxx')
browser.click('#submit');
});
it('Should be fill the form', function() {
browser.url(new_contact)
browser.waitForVisible('#addClient')
browser.setValue('#clientNameTextField-inputEl',name)
browser.setValue('#clientIdentidicationTextField-inputEl',ssn)
browser.setValue('#clientAddressTextField-inputEl','El busque')
browser.setValue('#clientCicyyTextField-inputEl','Valencia')
browser.setValue('#clientEmailField-inputEl','salvador.salvatierra@alegra.com')
browser.setValue('#clientPhoneTextField-inputEl','04141234567')
browser.setValue('[name="phone2"]','04147654321')
});
it('the contact is store',function() {
browser.click('=save)
browser.waitForExist('#viewClientInfoBalances')
browser.end;
});
});
});
第二个特征文件:
var url = 'http://someurl.com';
describe('We get the basic info from index contact', function(){
it('Shouldlogin to the system', function(){
browser.url(url)
browser.setValue('#email','xxxxxxxx@xxx.com')
browser.setValue('#password','xxxxx')
browser.click('#submit');
});
it('We should see the basic info', function(){
browser.click('[href="/client"]')
browser.click('#gridview-1043-record-ext-record-66 .action-icons a:nth-child(1)')
browser.waitForExist('#viewClientInfoBalances')
browser.end();
});
});
我看到三种不同方法的可能解决方案:
1. Create a login setup:
既然我看到你在使用 Mocha,那么我会在 .before()
中的所有测试用例之前 运行ning 你的登录片段] 挂钩:
describe("Whosebug Test Suite", function() {
before(function() {
return browser
.url(url);
.setValue('#email','xxxxxxxxxxx@xxxx.com')
.setValue('#password','xxxxxxxx')
.click('#submit');
});
it("\nYour first test...\n", function() {
return ...
});
it("\nYour second test...\n", function() {
return ...
});
});
Obs: .before()
钩子将 运行 ONLY ONCE,每个测试套件。如果您有不同的测试套件(describe
语句),其中每个测试用例都需要登录,请使用 .beforeEach()
挂钩。
更新!!!根据萨尔瓦多的要求,在评论区添加了这部分。
您有两种方法可以实现:
将你的登录移到wdio.config.js
beforeSuite
挂钩:
// Hook that gets executed before the suite starts
beforeSuite: function (suite) {
return browser
.url(url);
.setValue('#email','xxxxxxxxxxx@xxxx.com')
.setValue('#password','xxxxxxxx')
.click('#submit');
},
创建一个 main.js
文件,在其中注入所有 "modules"。您仅从该文件登录并通过 require
在其中注入所有 describe
填充的文件:
喷油器:
function importTest(name, path) {
describe(name, function() {
require(path);
});
}
main.js:
describe("All your tests go here!", function () {
// Executes its content before each imported feature
beforeEach(function() {
// beforeHooks
});
// Imported features/module files
importTest('Clients module', '../modules/clients.js');
//importTest('Devices module', '../modules/devices.js');
// Executes its content after all features have executed
after(function () {
// afterHooks
});
});
2. Loading a custom profile:
- 启动 WebdriverIO 测试用例,但在加载页面后添加
browser.debug()
;
- 转到您的网站并使用所需帐户登录。确保在浏览器中保存凭据;
- 现在我们必须保存此 自定义配置文件 并在每次启动 WebdriverIO 测试用例时加载它。在地址栏中输入 chrome://version。请注意 配置文件路径 值。复制文件夹的内容(例如:对于
C:\Users\<yourUserName>\Desktop\scoped_dir18256_17319\Default
,复制桌面上的 scoped_dir18256_17319 文件夹)。此文件夹包含 THIS[ 上的所有操作(搜索历史记录、安装的扩展程序、已保存的帐户/已保存的凭据 在我们的例子中) =110=] 当前实例;
现在我们需要做的就是在 wdio.config.js
文件中添加该文件夹的路径作为 chromeOptions
参数:
chromeOptions: {
//extensions: ['./browserPlugins/Avira-SafeSearch-Plus_v1.5.1.crx'],
args: [ '--user-data-dir=/Users/<yourUserName>/Desktop/scoped_dir18256_17319'
]
}
现在您所要做的就是 运行 使用此自定义配置文件的测试用例,您将使用首选的 username/password 组合登录。
Obs: 您可以阅读有关自定义配置文件的更多信息 HERE、使用自定义配置文件 部分。
3. Loading the authentication cookies (won't work on all websites)
- 使用所需的 username/password 组合登录您的网站;
- 打开 Chrome 控制台并转到 Applications 选项卡,在 Cookies 菜单中;
- 您将必须识别您的身份验证令牌(通常,所有网站都会在
cookies
中存储凭证等信息);
- 使用
.cookie()
或 .setCookie()
方法添加确切的 cookie(在您加载 URL 之后)。
代码应如下所示:
browser.setCookie({name: '<AuthCookieName>', value: '<AuthToken>'});
browser.refresh();
// Sometimes you have to refresh twice
browser.refresh();
// Assert you are logged in
参见 我对类似问题的回答作为示例。
希望对您有所帮助。干杯!
我想优化我的 WebdriverIO 测试。当我 运行 使用 WebdriverIO 的测试套件时,我试图避免重新输入用户名和密码。 (Chromedriver)
以下两个文件是一个模块的一部分,一共有4个模块。
第一个功能文件:
var name = 'Andrea' + Math.floor((Math.random() * 1000000) + 1);
var ssn = 'V-' + Math.floor((Math.random() * 1000000) + 1);
var url = 'http://someurl.com';
var new_contact = 'https://someurl.com/client/add';
describe('Some contact is create', function() {
it('Should login to the system', function() {
browser.url(url)
browser.setValue('#email','xxxxxxxx@xxxx.com')
browser.setValue('#password','xxxxxx')
browser.click('#submit');
});
it('Should be fill the form', function() {
browser.url(new_contact)
browser.waitForVisible('#addClient')
browser.setValue('#clientNameTextField-inputEl',name)
browser.setValue('#clientIdentidicationTextField-inputEl',ssn)
browser.setValue('#clientAddressTextField-inputEl','El busque')
browser.setValue('#clientCicyyTextField-inputEl','Valencia')
browser.setValue('#clientEmailField-inputEl','salvador.salvatierra@alegra.com')
browser.setValue('#clientPhoneTextField-inputEl','04141234567')
browser.setValue('[name="phone2"]','04147654321')
});
it('the contact is store',function() {
browser.click('=save)
browser.waitForExist('#viewClientInfoBalances')
browser.end;
});
});
});
第二个特征文件:
var url = 'http://someurl.com';
describe('We get the basic info from index contact', function(){
it('Shouldlogin to the system', function(){
browser.url(url)
browser.setValue('#email','xxxxxxxx@xxx.com')
browser.setValue('#password','xxxxx')
browser.click('#submit');
});
it('We should see the basic info', function(){
browser.click('[href="/client"]')
browser.click('#gridview-1043-record-ext-record-66 .action-icons a:nth-child(1)')
browser.waitForExist('#viewClientInfoBalances')
browser.end();
});
});
我看到三种不同方法的可能解决方案:
1. Create a login setup:
既然我看到你在使用 Mocha,那么我会在 .before()
中的所有测试用例之前 运行ning 你的登录片段] 挂钩:
describe("Whosebug Test Suite", function() {
before(function() {
return browser
.url(url);
.setValue('#email','xxxxxxxxxxx@xxxx.com')
.setValue('#password','xxxxxxxx')
.click('#submit');
});
it("\nYour first test...\n", function() {
return ...
});
it("\nYour second test...\n", function() {
return ...
});
});
Obs: .before()
钩子将 运行 ONLY ONCE,每个测试套件。如果您有不同的测试套件(describe
语句),其中每个测试用例都需要登录,请使用 .beforeEach()
挂钩。
更新!!!根据萨尔瓦多的要求,在评论区添加了这部分。
您有两种方法可以实现:
将你的登录移到
wdio.config.js
beforeSuite
挂钩:// Hook that gets executed before the suite starts beforeSuite: function (suite) { return browser .url(url); .setValue('#email','xxxxxxxxxxx@xxxx.com') .setValue('#password','xxxxxxxx') .click('#submit'); },
创建一个
main.js
文件,在其中注入所有 "modules"。您仅从该文件登录并通过require
在其中注入所有describe
填充的文件:
喷油器:
function importTest(name, path) {
describe(name, function() {
require(path);
});
}
main.js:
describe("All your tests go here!", function () {
// Executes its content before each imported feature
beforeEach(function() {
// beforeHooks
});
// Imported features/module files
importTest('Clients module', '../modules/clients.js');
//importTest('Devices module', '../modules/devices.js');
// Executes its content after all features have executed
after(function () {
// afterHooks
});
});
2. Loading a custom profile:
- 启动 WebdriverIO 测试用例,但在加载页面后添加
browser.debug()
; - 转到您的网站并使用所需帐户登录。确保在浏览器中保存凭据;
- 现在我们必须保存此 自定义配置文件 并在每次启动 WebdriverIO 测试用例时加载它。在地址栏中输入 chrome://version。请注意 配置文件路径 值。复制文件夹的内容(例如:对于
C:\Users\<yourUserName>\Desktop\scoped_dir18256_17319\Default
,复制桌面上的 scoped_dir18256_17319 文件夹)。此文件夹包含 THIS[ 上的所有操作(搜索历史记录、安装的扩展程序、已保存的帐户/已保存的凭据 在我们的例子中) =110=] 当前实例; 现在我们需要做的就是在
wdio.config.js
文件中添加该文件夹的路径作为chromeOptions
参数:chromeOptions: { //extensions: ['./browserPlugins/Avira-SafeSearch-Plus_v1.5.1.crx'], args: [ '--user-data-dir=/Users/<yourUserName>/Desktop/scoped_dir18256_17319' ] }
现在您所要做的就是 运行 使用此自定义配置文件的测试用例,您将使用首选的 username/password 组合登录。
Obs: 您可以阅读有关自定义配置文件的更多信息 HERE、使用自定义配置文件 部分。
3. Loading the authentication cookies (won't work on all websites)
- 使用所需的 username/password 组合登录您的网站;
- 打开 Chrome 控制台并转到 Applications 选项卡,在 Cookies 菜单中;
- 您将必须识别您的身份验证令牌(通常,所有网站都会在
cookies
中存储凭证等信息); - 使用
.cookie()
或.setCookie()
方法添加确切的 cookie(在您加载 URL 之后)。
代码应如下所示:
browser.setCookie({name: '<AuthCookieName>', value: '<AuthToken>'});
browser.refresh();
// Sometimes you have to refresh twice
browser.refresh();
// Assert you are logged in
参见
希望对您有所帮助。干杯!