我正在尝试将 cypress 和 1password 集成在一起,以实现更安全的自动化测试。我有一个测试,我登录、导航、注销平台,然后转移到存档并免费玩钱。我想使用环境变量进行相同的测试,从 1password 获取必要的登录数据。然而,测试运行初期就出现错误,并且无法获取我的凭据。
运行:“source ./fetchCredentials.sh echo $CYPRESS_EMAIL echo $CYPRESS_PASSWORD”实际上我确实获得了正确的凭据。
我还将下面的块添加到 json 文件中。
{
"scripts": {
"cy:run": "source ./fetchCredentials.sh && cypress run",
"cy:open": "source ./fetchCredentials.sh && cypress open"
}
}
我的 sh 文件来获取凭据
#!/bin/bash
# Fetch the email and password from 1Password using the 1Password CLI
email=$(op item get "Mogeniusßß" --vault="Employee" --fields username)
password=$(op item get "Mogeniusßß" --vault="Employee" --fields password)
# Check if the credentials were fetched successfully
if [ -z "$email" ] || [ -z "$password" ]; then
echo "Failed to fetch credentials from 1Password. Please ensure the item exists and the 1Password CLI is logged in."
exit 1
fi
# Export the fetched credentials as environment variables
export CYPRESS_EMAIL=$email
export CYPRESS_PASSWORD=$password
仅供参考,这是第一个运行顺利的测试,它不使用环境变量
describe('Login to Mogenius, perform actions, and then play a song', () => {
it('should log in, handle cookie consent, navigate, and log out of Mogenius', () => {
// Your actual login credentials
const email = '**********************';
const password = '********************';
// Visit the login page
cy.visit('https://app.dev.mogenius.com/user/login');
// Handle "Accept & Close" cookie consent button if it appears
cy.get('button#ppms_cm_agree-to-all').then(($btn) => {
if ($btn.is(':visible')) {
cy.wrap($btn).click(); // Click the "Accept & Close" button
}
});
// Enter the email using the placeholder attributex
cy.get('input[placeholder="Email"]').type(email);
// Enter the password using the placeholder attribute
cy.get('input[placeholder="Password"]').type(password);
// Click the login button
cy.get('button[type="submit"]').click();
// Assert that the login was successful by checking the URL or some element that appears after login
cy.url().should('include', 'https://app.dev.mogenius.com/studio/settings/organizations');
// Wait for the DemoOrg element to be visible and click it
cy.get('span.fw-bold.me-auto.text-truncate.flex-shrink-1').contains('DemoOrg').should('be.visible').click();
// Assert that the URL has changed to the DemoOrg page
cy.url().should('include', '/studio/organization/35bdd042-fcb7-4afd-af03-9feffe1dbae1');
// Wait for the JanGKE project element to be visible and click it
cy.get('span.h6.text-truncate').contains('JanGKE').should('be.visible').click();
// Wait for the EY element to be visible and click it
cy.get('div.container').contains('EY').should('be.visible').click();
// Wait for the Logout element to be visible and click it
cy.get('a.dropdown-item').contains('Logout').should('be.visible').click();
// Optionally, assert that the URL has changed to the login page
cy.url().should('include', 'app.dev.mogenius.com/user/login');
});
it('play Money for Nothing', {includeShadowDom: true, scrollBehavior: false}, () => {
// Set preferable screen resolution
cy.viewport(1500, 1500);
// Page base URL
cy.visit('https://archive.org/details/10.-telegraph-road-live-remix_202303');
// Handle any cookie consent or similar pop-ups on this page if they exist
//cy.get('button#ppms_cm_agree-to-all').then(($btn) => {
//if ($btn.is(':visible')) {
// cy.wrap($btn).click(); // Click the "Accept & Close" button
// }
//});
// Play "Money For Nothing" - Track #11
cy.contains('button', 'Money For Nothing').click();
});
});
我希望,在提供了上述详细信息后,cypress 能够顺利通过我的测试,如下:
describe('Login to Mogenius, perform actions, and then play a song', () => {
it('should log in, handle cookie consent, navigate, and log out of Mogenius', () => {
// Use the credentials from the environment variables
const email = Cypress.env('CYPRESS_EMAIL');
const password = Cypress.env('CYPRESS_PASSWORD');
// Ensure the variables are correctly loaded
//if (!email || !password) {
//throw new Error('CYPRESS_EMAIL or CYPRESS_PASSWORD is not set');
//}
// Visit the login page
cy.visit('https://app.dev.mogenius.com/user/login');
// Handle "Accept & Close" cookie consent button if it appears
cy.get('button#ppms_cm_agree-to-all').then(($btn) => {
if ($btn.is(':visible')) {
cy.wrap($btn).click(); // Click the "Accept & Close" button
}
});
// Enter the email using the placeholder attribute
cy.get('input[placeholder="Email"]').type(email);
// Enter the password using the placeholder attribute
cy.get('input[placeholder="Password"]').type(password);
// Click the login button
cy.get('button[type="submit"]').click();
// Assert that the login was successful by checking the URL or some element that appears after login
cy.url().should('include', 'https://app.dev.mogenius.com/studio/settings/organizations');
// Wait for the DemoOrg element to be visible and click it
cy.get('span.fw-bold.me-auto.text-truncate.flex-shrink-1').contains('DemoOrg').should('be.visible').click();
// Assert that the URL has changed to the DemoOrg page
cy.url().should('include', '/studio/organization/35bdd042-fcb7-4afd-af03-9feffe1dbae1');
// Wait for the JanGKE project element to be visible and click it
cy.get('span.h6.text-truncate').contains('JanGKE').should('be.visible').click();
// Wait for the EY element to be visible and click it
cy.get('div.container').contains('EY').should('be.visible').click();
// Wait for the Logout element to be visible and click it
cy.get('a.dropdown-item').contains('Logout').should('be.visible').click();
// Optionally, assert that the URL has changed to the login page
cy.url().should('include', 'app.dev.mogenius.com/user/login');
});
it('play Money for Nothing', { includeShadowDom: true, scrollBehavior: false }, () => {
// Set preferable screen resolution
cy.viewport(1500, 1500);
// Page base URL
cy.visit('https://archive.org/details/10.-telegraph-road-live-remix_202303');
// Play "Money For Nothing" - Track #11
cy.contains('button', 'Money For Nothing').click();
});
});
不幸的是我收到以下错误:
6
类型
(xhr)POST https://px.ads.linkedin.com/wa/
赛普拉斯错误
cy.type() 只能接受字符串或数字。您已通过:
undefined
了解更多
cypress/e2e/1-getting-started/env2.cy.js:23:46
21 |
22 | // Enter the email using the placeholder attribute
> 23 | cy.get('input[placeholder="Email"]').type(email);
| ^
24 |
25 | // Enter the password using the placeholder attribute
26 | cy.get('input[placeholder="Password"]').type(password);
运行令人困惑:source ./fetchCredentials.sh
回显$CYPRESS_EMAIL
echo $CYPRESS_PASSWORD 确实给了我真实的凭据,但不知何故它们在测试运行期间没有被获取。
非常感谢您的帮助
来自 覆盖环境变量
Cypress 会自动从以此方式指定的任何环境变量名称中删除前导
或CYPRESS_
。cypress_
因此,在外部设置
CYPRESS_EMAIL
和 CYPRESS_PASSWORD
时,您希望在测试代码中使用以下内容:
const email = Cypress.env('EMAIL');
const password = Cypress.env('PASSWORD');