Cypress - alerts, Date Picker, tooltips

복정규·2022년 10월 12일
0

cypress

목록 보기
4/4

이벤트 사용 방법

cy.on("window:alert", (str) => {
  expect(str).to.equal("I am a JS Alert");
});
cy.on("window:confirm", () => true);

Alert

alert test: https://the-internet.herokuapp.com/javascript_alerts

// 시나리오
1. click for js Alert 버튼을 찾아서 클릭
2. alert창에서 텍스트가 맞는지 확인
3. alert에서 확인 클릭이벤트
4. 아래 result 텍스트에 "You successfully clicked an alert" 텍스트 확인

describe("alerts", () => {
  beforeEach(() => {
    cy.visit('https://the-internet.herokuapp.com/javascript_alerts')
  })

  it('alert', () => {
    cy.get('button').contains('Click for JS Alert').click()
    cy.on("window:alert", (str) => {
      expect(str).to.equal("I am a JS Alert");
    });
    cy.on("window:confirm", () => true);
    cy.get("#result").should("have.text", "You successfully clicked an alert");
  })
  
  it("JS Confirm(accept)", () => {
    cy.contains("Click for JS Confirm").click();
    cy.on("window:confirm", (str) => {
      expect(str).to.equal(`I am a JS Confirm`);
    });
    cy.on("window:confirm", () => true);
    cy.get("#result").should("have.text", "You clicked: Ok");
  });
  
  it("JS Confirm(cancel)", () => {
    cy.contains("Click for JS Confirm").click();
    cy.on("window:confirm", (str) => {
      expect(str).to.equal(`I am a JS Confirm`);
    });
    cy.on("window:confirm", () => false);
    cy.get("#result").should("have.text", "You clicked: Cancel");
  });
  
  it("JS Prompt", () => {
    // window() 는 현재 활성 상태인 페이지의 창 개체를 가져옵니다.
    cy.window().then(($win) => {
      //Control prompt behavior
      cy.stub($win, "prompt").returns("This is a test text");
      cy.contains("Click for JS Prompt").click();
    });
    cy.get("#result").should("have.text", "You entered: This is a test text");
  });
})
// 독립적인 stub를 생성한다. (보통 유닛테스트에 사용)
cy.stub()
// obj.method()를 stub화 한다.
cy.stub(obj, 'method')
// obj.method()가 "woowa"를 반환하도록 한다.
cy.stub(obj, 'method').returns('woowa')
// obj.method()가 'haru'인자와 함께 호출되었을 때 "woowa"를 반환하도록 한다.
cy.stub(obj, 'method').withArgs('haru').returns('woowa')

App Events

https://docs.cypress.io/api/events/catalog-of-events#App-Events

Cypress Events

https://docs.cypress.io/api/events/catalog-of-events#Cypress-Events

작성중...

profile
프론트엔드 개발자 입니다.

0개의 댓글