i am getting this error when i run a test file. The test file uses methods declared in a page object file, please let me know how this can be fixed.
Error message
Page Object code
cy.get('.more-less-container').then(dropdown => {
if (dropdown.find('.name').contains('More')) {
cy.get('more-less-container').click();
} else {
console.log('folders are in expanded state');
}
});
class Folders {
Archive() {
dropdown();
cy.get('.category-content')
.find('[title="Archive"]')
.click()
.get('.folder-details')
.should('contain.text', 'Archive');
}
Trash() {
dropdown();
cy.get('.category-content')
.find('[title="Trash"]')
.click()
.get('.folder-details')
.should('contain.text', 'Trash');
}
Test code
/// <reference types="cypress" />
import { openFolder } from '../support/page-objects/sidebar/drafts';
describe('Verify mobile app download link redirections', () => {
before(() => {
cy.login();
});
});
it('Needs to open the drafts folder', () => {
openFolder.Drafts();
cy.get('.modal-close').click();
});
Not sure what i'm missing out here.
As I can see the cy.get(...) command (line 1) in your "Page Object code" file is not part of a class, and as such will be executed as soon as cypress is opened (ran), because that's how this support file is supposed to work, it's loaded and executed immediately (which means before any test are actually started). So the solution would be to move that command as part of the Folders class as a utility function, which later on you would explicitly call during your tests.
Related
In my test i have four links. Each of them redirect to the different page. After redirection I have page with breadcrubms. In my test I would like to assert that after redirection the breadcrumbs contain specific text which are defined in fixtures. How can I made it?
You could add a custom Cypress command to validate your breadcrumbs, and drive the behavior based on the url yielded by cy.url().
Cypress.Commands.add('validateBreadcrumbs', () => {
cy.url().then((url) => {
if (url === 'foo') {
// some code
} else if (url === 'bar') {
// some other code
} else {
// some other code
}
});
});
// in a test
it('test', () => {
// code to hit the re-direct
cy.validateBreadcrumbs();
});
That being said, it may just be simpler and more direct to not have an abstracted function, and just validate the text as you normally would after the re-direction.
I am testing a website i.e console.internetcomputerservices.com, please refer the below code:
/// <reference types="cypress" />
describe("Basic tests", () => {
before(() => {
cy.visit("https://console.internetcomputerservices.com", {
onBeforeLoad(win) {
win.localStorage.setItem(
"token",
"my-token"
);
},
});
});
it.only("should be able to run test 1", async () => {
cy.viewport(1280, 720)
//Clicks on Creat DApp
cy.contains('Create DApp').click()
//Clicks on Static website
cy.contains('Static Website').click()
//clicks on "Connect with github"
cy.contains('Connect with github').click()
//The origin is changed to github.com inside which following operation takes place
cy.origin('https://github.com', ()=>{
cy.get('input[name="login"]').type('my-email')
cy.get('input[name="password"]').type('my-password')
cy.get('[type="submit"]').click()
cy.get('[href="/settings/installations/29576211"]').click()
cy.contains('Only select repositories').click()
cy.contains('All repositories').click()
cy.get('.js-integrations-install-form-submit.btn-primary.btn.float-left.mr-2').click()
})
//Comes out of github.com back to console.internetcomputerservices.com
//(Expected) cypress will identify the placeholder and type "intro-2"
// but this doesn't happen, cypress doesn't identify any element inside the DOM
cy.get('input[placeholder="Search…"]').type('intro-2')
});
});
When we return back to our home website, i expect that the cypress identifies the placeholder and type in "intro-2", to check that was it particular to placeholder, i realised that cypress is not able to identify any element on DOM.
If you refer the Search bar, i want to access one with placeholder='Search...'
I apologies for poor choice of words, i am beginner in this and not familiar with jargon, I hope you understand my question.
I'm using the cy.visit() command but the website i'm visiting (which i don't own) doesn't always fire the load event, although the content itself that i need for testing does appear on the website.
Despite the content appearing, since the load event is not fired sometimes (for some reason which i can't fix since i don't have ownership over this website), the cy.visit() command fails.
Is there a way to "force" it somehow, similar to how we can pass { force: true} for the cy.click() command?
Add the below to your cypress commands file
Cypress.Commands.add('forceVisit', url => {
cy.window().then(win => {
return win.open(url, '_self');
});
});
And in your tests, you can call
cy.forceVisit("www.google.com")
It's hard to simulate the problem, but I think I managed by setting pageLoadTimeout really low (30ms).
You can catch the onLoad fail in an event handler and checking for the page load error message.
I recommend doing it in a beforeEach().
beforeEach(() => {
Cypress.config("pageLoadTimeout", 30) // set this to whatever time length
// you feel is appropriate to start testing
// You'll need to experiment to get this right
// and in CI it will be a lot longer
cy.once('fail', (err) => { // "once" to just catch a single error
const message = err.parsedStack[0].message
if (message.match(/Timed out after waiting `\d+ms` for your remote page to load/)) {
return false
}
throw err // any other error, fail it
})
cy.visit('www.example.com');
})
it('checks the heading of the page', () => {
cy.get('h1').should('have.text', 'Example Domain') // ✅
})
As you can already assume, that is highly discouraged. It also really depends on how it fails and with which errors, but, without any code to reproduce, you may want to try this if you haven't already:
cy.visit('/', {failOnStatusCode: false});
Reference: https://docs.cypress.io/api/commands/visit#Arguments
Like in JUnit we have category annotation where we define regression, sanity name and set this category in pom.xml file to run only those Testcases. We dont need to change anything afterwards.
can we do same in Jasmine Protractor???
If we do have one file which is firstfile.ts
describe('angular-material paginator component page', () => {
const EC = protractor.ExpectedConditions;
beforeAll(async() => {
await browser.get('https://material.angular.io/components/paginator/examples');
});
it('Should navigate to next page', async() => {
await $('button[aria-label=\'Next page\']').click();
});
it('Should navigate to previous page', async() => {
await $('button[aria-label=\'Previous page\']').click();
});
it('Should change list length to 5 items per page', async() => {
await $('mat-select>div').click();
});
});
like this we have one more spec file and i want to set categories of the it block so i can write that only one word and run the test like in JUnit.
other than the option of f and x before describe and it block.
For annotation based execution you can try BDD in protractor with cucumber.
Here you can create features which can be organised and executed.
Refer this github repo : https://github.com/igniteram/protractor-cucumber-typescript.git
Please try to clone this repo and understand how it works.
I am trying to test a flow
1.Ajax Request > Loader is visible
2.Response Received > a.Loader is hidden
b.Redirect to another page(where a interstitial is visible)
White testing them with casperJS I use the waitFor method, something like this.
casper.waitFor(function check() {
return this.evaluate(function() {
return $("#loader").is(":hidden");
});
}, function then() {
this.test.pass("Ajax request");
this.waitDone();
}, function timeout() { // step to execute if check has failed
this.echo("Timeout: page did not load in time...").exit();
},4000);
The thing is even if the condition is passed in check, then is not executed until the page is not redirected(read the flow, I am trying to test) and the test suite won't move to the next step.
Is there something I am missing here ?
Make sure your reference 'this' is located within a casper block:
casper.then(function() {
this.waitFor(function check() {
Possible quick fix without knowing more detail. Pass jQuery variable to evaluate.
this.evaluate(function($) {
You could also try:
casper.waitWhileVisible('#loader', function() {
// executes when #loader is hidden
});
docs located at: CasperJS waitWhileVisible