You can use Playwright's APIRequestContext class to log in via HTTP requests instead of form filling.
First, create an instance of APIRequestContext by calling browser.newContext()
. Pass in an object with context options like httpCredentials
for authentication and proxy
for a proxy server setup.
const context = await browser.newContext({
httpCredentials: { username: 'myusername', password: 'mypassword' },
});
Next, use the fetch()
method of APIRequestContext to send HTTP requests. First, send a GET request to retrieve any necessary cookies or tokens for authentication.
const page = await context.newPage();
await page.goto('https://ray.run/login');
// Retrieve anti-forgery token from cookie
const cookies = await context.cookies();
const xsrfToken = cookies.find(c => c.name === 'XSRF-TOKEN').value;
Finally, include these values as headers or query parameters in your POST request with login credentials.
// Log in using POST request
await context.fetch('https://ray.run/login', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'X-XSRF-TOKEN': xsrfToken,
},
data: {
username: 'myusername',
password: 'mypassword',
},
});
In short, logging in with a request object involves creating an APIRequestContext instance with appropriate options.
If you still have questions, please ask a question and I will try to answer it.
Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].