Quantcast
Channel: Node.jsタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 8691

【備忘録】puppeteerでurl取得

$
0
0

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

URL取得する方法メモ。
現在ページのURLが期待値と比較したいけど、なぜか取得できない。

url取得できない例
letpuppeteer=require("puppeteer");letbrowser;letpage;beforeAll(async()=>{browser=awaitpuppeteer.launch({args:["--disable-web-security"],headless:false,slowMo:30});page=awaitbrowser.newPage();jest.setTimeout(20000);});afterAll(()=>{browser.close();});describe("TEST",()=>{test("toMypage",async()=>{// 画面移動awaitpage.goto("http://localhost:8000/mypage");// マイページへ遷移成功awaitpage.waitForTimeout(5000);// location.hrefで完全なURL取得できるから、比較するawaitexpect(location.href).toEqual("http://localhost:8000/mypage");awaitpage.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()にするawaitexpect(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
で既出でした。


Viewing all articles
Browse latest Browse all 8691

Trending Articles