question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to pass page object to a function in another file.

See original GitHub issue

I have a file index.js where i have initialized the browser as below :

(async () => {
    const browser = await _driver.launch
    (
        {
            headless: false,
            args: ['--start-maximized']
        }
    );
    var page = await browser.newPage();
    await page.setViewport({'width': 1366, 'height': 768 });    
    await page.goto('https://www.example.com/');
})();

Now i have another file homepage.js which contains the methods for manipulations on the homepage. This file used the page object initialized in index.js file. Im trying to pass the page object to the function of homepage.js bt it gives me UnhandledPromiseejectionWarning.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:14 (7 by maintainers)

github_iconTop GitHub Comments

7reactions
vsemozhetbytcommented, Jan 18, 2019

Something like this? index.js:

'use strict';

const _driver = require('puppeteer');

module.exports = async () => {
  const browser = await _driver.launch({
    headless: false,
    args: ['--start-maximized']
  });
  var page = await browser.newPage();
  await page.setViewport({ 'width': 1366, 'height': 768 });
  await page.goto('https://www.example.com/');
  return page;
};

homepage.js:

'use strict';

const getPage = require('./index.js');

(async function main() {
  try {
    const page = await getPage();
    console.log(await page.evaluate(() => document.title));
  } catch (err) {
    console.error(err);
  }
})();

Output:

Example Domain
3reactions
vsemozhetbytcommented, Jan 19, 2019

index.js:

'use strict';

const puppeteer = require('puppeteer');
const pUtils = require('./puppeteer-utils.js');

(async function main() {
  try {
    const browser = await puppeteer.launch();
    const [page] = await browser.pages();

    await page.goto('https://example.org/');

    console.log(await pUtils.getLocation(page));
    console.log(await pUtils.getTitle(page));

    await browser.close();
  } catch (err) {
    console.error(err);
  }
})();

puppeteer-utils.js:

'use strict';

async function getLocation(page) {
  const location = await page.evaluate(() => document.location.href);
  return `Document location is "${location}".`;
}

async function getTitle(page) {
  const title = await page.evaluate(() => document.title);
  return `Document title is "${title}".`;
}

module.exports = {
  getLocation,
  getTitle,
};

Output:

Document location is "https://example.org/".
Document title is "Example Domain".
Read more comments on GitHub >

github_iconTop Results From Across the Web

Cant we pass object to write function to add data in one file to ...
.write method of file object takes string type variable only as arguments. Passing it another file object will not work.
Read more >
Stop using Page Objects and Start using App Actions - Cypress
In this post I argue that page objects are a bad practice, and suggest dispatching actions directly to the application's internal logic.
Read more >
Page object operations - Provar
A page object operation is a custom function that is used to execute Java code on a page object within Provar.
Read more >
Write a Selenium Java Test Suite Using Page Object Model
The first method, public Login(WebDriver driver), is a constructor that will run whenever a new instance of the class is created. This class...
Read more >
Page Object Model In Playwright | LambdaTest - YouTube
Watch this video to learn about the Page Object Model (POM) design ... each web page of an application as a different class...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found