2

This attached image is my HTML code for href which will open a new tab

The DOM has iframe so I wrote below code accessed the href and it will open in new tab. I am unable to access the newly opened tab, though I know the method that would have target attribute so we remove that and open in same tab but here I don't have any target attributes.

Please check this and help to access my new tab.

cy.visit('https://yopmail.com/en/')
cy.get('.ycptinput').type('some_name {enter}')
cy.wait(2000)
cy.get('#ifmail').its('0.contentDocument.body').then(cy.wrap).find('a').click()
  • Have you tried to extract the href from the `` tag and visit it? – jjhelguero Jul 15 '22 at 13:54
  • Yes I did, used cy.visit() then showed me to use cy.origin() since this is another domain. After using cy.origin() it showing me the error "cy.origin() requires the last argument to be a function. You passed: ` ` . I am not sure how to solve this. – Mr.Prasad J Jul 15 '22 at 14:43
  • Have you read the documentation on using `cy.origin()`? https://docs.cypress.io/api/commands/origin – DJSDev Jul 15 '22 at 15:18
  • Kindly note this, Everybody would have read the documentation and gone through other sources to find a solution. We post a question here to get the suggestions and clear answers. If you can help, take me to write a correct script. @DJSDev – Mr.Prasad J Jul 16 '22 at 04:58
  • Apologies if it came off as rude, but your previous comment quite literally asked how to solve a usage problem, which would be remedied by reading the documentation – DJSDev Jul 17 '22 at 15:21

1 Answers1

2

The cy.origin() command is meant to solve the "new tab" problem.

It's a bit new, so expect some teething problems. Basically, it sets up a sand-boxed domain that can use Cypress commands.

Anything from outside cy.origin() that you want to use inside (for example, the link you found) needs special handling to pass in.

It gets passed in on a special args option, and is received in the same pattern.

let link;

cy.visit('https://yopmail.com/en/')
cy.get('.ycptinput').type('some_name {enter}')
cy.wait(2000)
cy.get('#ifmail').its('0.contentDocument.body')
  .then($body => {
    link = $body.find('a')[0].href
  })

cy.then(() => {          // this just waits for above block to complete

  const newOrigin = link.split('?')[0]  // remove query params 
      .replace('http://', 'https://')   // correct for secure protocol

  cy.origin(newOrigin, { args: { link } }, ({ link }) => {

    cy.visit(link)       // same as ".find('a').click()" but works cross-domain
  })
})
    

Fody
  • 23,754
  • 3
  • 20
  • 37
  • Thanks, I have already tried this kind of solution and also I tried yours but I am getting an error as "cy.origin() requires the first argument to be either a url (https://www.example.com/path) or a domain name (example.com). Query parameters are not allowed." I am not sure this is because of href or something. – Mr.Prasad J Jul 18 '22 at 08:47
  • What does `link` contain? It should be a string when you use `link = $body.find('a')[0].href` – Fody Jul 18 '22 at 08:53
  • Reading the message a bit more carefully, I think the link needs to be split at the `?` character. I will change the code to do that. – Fody Jul 18 '22 at 09:01
  • Your solution seems working but I think problem is with url="http://url6287.wholesoftmarket.com/ls/click?upn=-2Fb58LO4QsdgvwfwNo3TX-2FJ1pyc-2FY8J1TFUqLL2k48mHZ0HK8Jko78fSWfdugy293AtAkRThRwqMu0Xyd9wS5Udj5S31MNGAbYlQw3NtgR0Q-3D0Opo_Fmyjk-2FjVnGSK5ZdUISbmGcj9UKWrx-2B-2BioyzIPGN1SYpR1gbZ-2BGD9IXpgZCw9o712cMISESf5SbP01Hj9rhmbrB2fPacV-2Bmg4xJb-2B-2B-2FUZ7DbITsHJJLlT-2F1kKxz7w6730jCruxPDzHV4mC2vPz8gmN65CLmvDprzwoOyFVQRZJA7NEDH8Kp3-2BW4mXk03C5clQvWGwxV36v6OXYLn3m0cUr3-2FyfxFuujGnq5xioW1P4Ik-3D". It keeps telling me that "You may only cy.visit() same-origin URLs within cy.origin()". – Mr.Prasad J Jul 18 '22 at 10:45
  • It's working when I hardcode the url inside cy.origin('https://dev.wholesoftmarket.com/memberReg/62cfb2a5f35aff002c8a4773', args, ()=>{}) but not by using newOrigin variable. It throwing an error The previous URL you visited was: 'http://wholesoftmarket.com' and You're attempting to visit this URL: 'https://dev.wholesoftmarket.com' – Mr.Prasad J Jul 18 '22 at 10:50
  • 1
    Yeah I tried it out on my own link - it's not enough just to `.split('?')`, some links are lazy about the protocol (gives `http://` but browser corrects to secure `https://`). Can happen in older page that was originally written without `https://`. Now-days chrome auto-corrects, so problem is never caught. But `cy.origin()` is in beta still, no doubt they will eventually auto-correct that as well. I added extra code above to handle it. – Fody Jul 19 '22 at 02:04
  • Wow that's great! It's working clearly and you explained the way of solution beautifully, Thank you so much. – Mr.Prasad J Jul 19 '22 at 07:16
  • Your'e welcome, thanks for your own help in getting the solution. – Fody Jul 19 '22 at 07:57