Cypress interact with Metamask Extension - google-chrome-extension

I have a website related to blockchain. I am using cypress to execute automation testing for this site. I have problem when cypress interact with Metamask extension.
Embeeded chrome in Cypress can load Metamask extentsion but
With first time using: I have to input seed phase by hand 1 time before.
With other times: I have to input password by hand before
My question: How to configure cypress auto fill password or seed phase automatically.
My code is bellow:
cypress.config.ts:
...
on('before:browser:launch', (browser = {}, launchOptions) => {
launchOptions.extensions.push('D:/Research/META3/metamask-chrome-10.15.0')
// How to set password /seedpharse for Metamask extentsion.
return launchOptions
})

Related

How to anbale cookies for gitlab OAuth when running cypress test

I have an angular app which has to authorize to gitlab and signIn the user with userName and pass in order to fetch some data/information from gitlab and display it in the UI.
The first test case fails before the browser can redirect to the gitlab login-form (https://gitlab.com/users/sign_in):
Spec Code:
it('Visits the home page', () => {
cy.visit('/', {
onBeforeLoad(win) {
cy.stub(win.navigator, 'cookieEnabled', false).as('cookieEnabled');
},
})
})
Error:
Checking your browser before accessing gitlab.com.
Please enable Cookies and reload the page.
This process is automatic. Your browser will redirect to your requested content shortly.
Please allow up to 5 seconds…
I have been searching and trying couple of other options/implementation based on cypress official doc etc., but still facing the same issue. Some kind of similar questions here in SO, but none for the hints/answers provide the fix.
Also I am not 100% sure that it's really an issue related to cypress test implementation. Could it be an issue affecting gitlab itself?
Any ideas how to fix such issue?
Cypress: 10.7.0
Angular: 14.x
Node: v16.13.1

Is there a way to connect to my existing browser session using playwright

I wish to connect to a website and download some pdf files. The website allows us to view the content only after log in. It asks us to log in using OTP and can't be login at more than 3 devices simultaneously.
I wish to download all the pdf listed. So I previously tried the
python playwright open --save-storage websitename.json
to save the login. But it doesn't work for that specific website.
The website.json file was empty whereas it worked for other websites.
Therefore the only solution I could think of know, is to connect to the current browser, open that website and then download those pdfs.
If you have some solution for this or even some other approach please do inform.
I was also thinking about switching over to puppeteer for the same.
But, I don't know the html parsing using node.js, since I feel using css selectors more comfortable, so I can't switch it.
Playwright is basically same as Puppeteer. So it wouldn't be a problem if you switch between the two.
You can use puppeteer-core or playwright to control your existing browser installation, for example Chrome, and then use the existing user data (Profile) folder to load the specified website login info (cookies, webstorage, etc).
const launchOptions = {
headless: false,
executablePath: '/Applications/Google Chrome/Contents/MacOS/Google Chrome', // For MacOS
// executablePath: 'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe', // For Windows
// executablePath: '/usr/bin/google-chrome' // For Linux
args: [
'--user-data-dir=/Users/username/Library/Application Support/Google/Chrome/', // For MacOS
// '--user-data-dir=%userprofile%\\AppData\\Local\\Chrome\\User Data', // For Windows
// '--profile-directory=Profile 1' // This to select default or specified Profile
]
}
const puppeteer = require('puppeteer-core')
const browser = await puppeteer.launch(launchOptions)
For more details about Playwright's method, you can check this workaround:
https://github.com/microsoft/playwright/issues/1985
To connect to an already running browser (Chrome) session, you can use connect_over_cdp method (added in v1.9 of playwright).
For this, you need to start Chrome in debug mode. Create a desktop shortcut for Chrome and edit Target section of shortcut properties to start it with debug mode. Add --remote-debugging-port=9222 to the target box in shortcut properties so that the target path becomes:
C:\Program Files\Google\Chrome\Application\chrome.exe" --remote-debugging-port=9222
Now start Chrome and check if it is in debug mode. For this open a new tab and paste this url in the address bar: http://localhost:9222/json/version. If you are in debug mode, you should see now a page with a json response, otherwise if you are in "normal" mode, it will say "Page not found" or something similar.
Now in your python script, write following code to connect to chrome instance:
browser = playwright.chromium.connect_over_cdp("http://localhost:9222")
default_context = browser.contexts[0]
page = default_context.pages[0]
Here is the full script code:
# Import the sync_playwright function from the sync_api module of Playwright.
from playwright.sync_api import sync_playwright
# Start a new session with Playwright using the sync_playwright function.
with sync_playwright() as playwright:
# Connect to an existing instance of Chrome using the connect_over_cdp method.
browser = playwright.chromium.connect_over_cdp("http://localhost:9222")
# Retrieve the first context of the browser.
default_context = browser.contexts[0]
# Retrieve the first page in the context.
page = default_context.pages[0]
# Print the title of the page.
print(page.title)
# Print the URL of the page.
print(page.url)

Poltergeist JS/Headless Chrome - Switch to offline mode

I am looking to write a test where I can switch between Offline mode and back to Online mode mid way through a cucumber test. I can manually achieve this via Dev Tools in Chrome but is there a way to automate this using Poltergeist JS or Headless Chrome.
I know that page.driver is accessible, infact I use this for setting cookie values in another test
Given(/^I set the "([^"]*)" cookie value to "([^"]*)" for the domain "([^"]*)"$/) do |cookieName,cookieValue,cookieDomain|
if "#{DRIVER}" == "headless_chrome"
page.driver.browser.manage.add_cookie name: cookieName, value: cookieValue, domain: cookieDomain
else
page.driver.set_cookie(cookieName, cookieValue, {:domain => cookieDomain})
end
sleep 1
end
Unless I'm missing something I can't see how to switch between Offline and Online modes. Anyone done or do this in their test setup?
When using Selenium with Chrome as the driver you can use network_conditions=
page.driver.browser.network_conditions = { offline: true }
I don't believe Poltergeist had similar functionality.

How to use Firebase Auth's Google Sign-In option with Cypress.io

I need help using Google Sign-In when testing my Angular 6 app with Cypress. It can't use the sign-in popup, and so I'm trying to follow Cypress' advice to "always use cy.request() to talk to 3rd party servers via their APIs." That's from https://docs.cypress.io/guides/references/best-practices.html#Visiting-external-sites which then points us to this example: https://github.com/cypress-io/cypress-example-recipes/blob/master/examples/logging-in__single-sign-on/cypress/integration/logging-in-single-sign-on-spec.js - more info on how to do this is seen at minute 23 in a presentation by the Cypress author: https://www.youtube.com/watch?v=5XQOK0v_YRE
I'm taking the video solution and trying to modify it for Firebase Auth according to https://firebase.google.com/docs/auth/web/google-signin#advanced-authenticate-with-firebase-in-nodejs but I'm getting stuck on how to obtain the proper id_token and so far I have this code in my commands.js file:
Cypress.Commands.add('login', () => {
var id_token = ___?____;
cy.request({
method: 'POST',
url: 'https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyAssertion?key=[API_KEY]',
body: {
"requestUri": "http://localhost:3500",
"postBody": `id_token=${id_token}&providerId=google.com`,
"returnSecureToken": true,
"returnIdpCredential": true
}
})
})
I believe I need to use a different API to initiate the login and request user credentials, which will include the id_token, and so I tried https://firebase.google.com/docs/auth/web/google-signin#advanced-authenticate-with-firebase-in-nodejs but am not yet skilled enough to pull in external js files (https://apis.google.com/js/platform.js) into Node (This is probably not possible in js files the way it is in html). Using this package: https://github.com/google/google-auth-library-nodejs may be my next attempt. Is there anyone who can pick it up from here?
Similar question at Is it possible to use Cypress e2e testing with a firebase auth project? - but they are signing in with user and pass.

Make multiple page Logins with WebDriver Selenium using Groovy and Spring Boot

i'm using Geb Selenium with Phantomjs Driver, realy using a RemoteWebDriver for interact with a web page using the phantomjs running over a console, i'm using also Spring boot so i call my Groovy Geb scripts from a REST method, what i want is to, for example, running scripts that login two different users asynchronously and make some work in their accounts, but when i run my code is like the first loged user is used over the second, i check in my code if the user is loged then go to his account, is like Selenium or Phantomjs uses an only one Browser for all my request so when i do a request for login and then make another one the first login is presents, is there a way for run my request in an isolated browser or driver in my case, or maybe is because some kind of cookies?
this is the code where i do my login using Groovy geb (called from a REST method in Spring):
public void test_make_login(String login, String password){
def String imgUrl = null
Browser.drive {
to MyPage
makeLogin(login,password){
println page
imgUrl = profileThumbnail.attr("src")
}{
imgUrl = "none"
}
println imgUrl
}
}
I'm using Angular $resource for make my REST requests, in my program i want to get the profile loged picture of each user but all users haves the first loged accound picture.

Resources