I'm faced with a unique challenge where I need to extract data from an external source and incorporate it into my base URL. How can I remove the aliases that are causing errors whenever I try to call them? https://i.sstatic.net/gBmBW.png
Below is the script snippet that includes cy.origin()
:
// code snippet from the scenario
const credentials = {
username: username,
password: password,
}
cy.origin(Cypress.env(‘other’Url), { args: credentials }, ({ username, password }) => {
cy.visit('/')
cy.get('input[id="session_username"]').type(username)
cy.get('input[id="session_password"]').type(password)
cy.get('input[value="Log in"]').click()
cy.wait(3000)
// fetch details
cy.get(‘<firstElementLocator>’).invoke('text').then((firstAlias) => {
cy.wrap(firstAlias).as('firstAlias')
})
cy.get('<secondElementLocator>').invoke('text').then((secondAlias) => {
cy.wrap(secondAlias).as('secondAlias')
})
})
// navigate to baseUrl
cy.visit('/')
cy.get(‘@firstAlias’).then((firstAlias) => {
cy.get(‘@secondAlias’).then((secondAlias) => {
var firstData = String(firstAlias)
var secondData = String(secondAlias)
cy.get(‘<thirdElementLocator>’).type(firstData)
cy.get(‘<fourthElementLocator>’).type(secondData)
})
})
Having considered @Fody's response (thank you Fody), I managed to resolve the error within the scripts. However, when I attempted to execute them, I encountered the following issue:
https://i.sstatic.net/afE64.png
Take a look at the configuration script: cypress.config.ts
let data: any
let key: any
let value: any
module.exports = defineConfig({
e2e: {
async setupNodeEvents(on: (arg0: string, arg1: any) => void, config: any) {
// implement node event listeners here
const bundler = createBundler({
plugins: [createEsbuildPlugin(config)],
});
on('task', {
setValue(key:any, value:any) {
data[key] = value
return null
},
getValue(key:any) {
return data[key] || null
},
})
on('file:preprocessor', bundler);
await addCucumberPreprocessorPlugin(on, config);
return config;
},
experimentalSessionAndOrigin: true,
Step implementation:
cy.get('table > tbody > tr:nth-child(1) > td:nth-child(2)').invoke('text').then((generatedSMN) => {
cy.task('setValue', { key: 'generatedSMN', value: String(generatedSMN) })
})