0
1

More than 3 years have passed since last update.

【備忘録】puppeteerでurl取得

Last updated at Posted at 2021-01-08

ご無沙汰してます。おおのんです。

URL取得する方法メモ。
現在ページのURLと期待値を比較したいけど、どうすれば取得できるのだろう。

url取得できない例
let puppeteer = require("puppeteer");
let browser;
let page;

beforeAll(async () => {
  browser = await puppeteer.launch({
    args: ["--disable-web-security"],
    headless: false,
    slowMo: 30
  });
  page = await browser.newPage();
  jest.setTimeout(20000);
});

afterAll(() => {
  browser.close();
});

describe("TEST", () => {
  test("toMypage", async () => {
    // 画面移動
    await page.goto("http://localhost:8000/mypage");
    // マイページへ遷移成功
    await page.waitForTimeout(5000);
    // location.hrefで完全なURL取得できるから、比較する
    await expect(location.href).toEqual("http://localhost:8000/mypage");
    await page.close();
  });
});
結果
● LOGIN TEST › Login
    expect(received).toEqual(expected) // deep equality
    Expected: "http://localhost:8000/mypage"
    Received: "http://localhost/"

・・・アカン。
Received: "http:/localhost/"になる。

こうすると取得できる。

取得できる例
// ~略~
  // location.href => url.path()にする
    await expect(url.path()).toEqual("http://localhost:8000/mypage");
// ~略~
結果
● LOGIN TEST › Login
    Test Suites: 1 passed, 1 total
    Tests:       1 passed, 1 total

通った~。

【puppeteer/puppeteer】 how to get current url ? #2215
で既出でした。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1