1

我一直在尝试获取当前的 URL,并希望删除部分 URL 以在另一种方法中使用。我尝试了很多方法,我在网上看到的一个解决方案建议使用该beforeEach()方法。我一直在尝试这个,无论如何,this.urlString总是返回未定义。

这是我目前拥有的。我必须遗漏一些关于为什么返回未定义但我无法弄清楚的东西。

this.urlString;

beforeEach(function () {
  mainPO.navigateHome();
  mainPO.clickCurrent();

  browser.getCurrentUrl().then(function (url) {
    return this.urlString = url;
  });
});

console.log(this.urlString)

我想要做的是将 URL 存储在字符串中,然后解析字符串以删除 URL 的“.com”之前的所有内容,这样我就可以将字符串插入另一个 URL 以爬取到另一个链接。

4

1 回答 1

2

为了将 URL 值作为字符串获取,您需要解析函数调用返回的承诺browser.getCurrentUrl()

beforeEach(function () {
  mainPO.navigateHome();
  mainPO.clickCurrent();

  this.urlString = browser.getCurrentUrl();
});

this.urlString.then(function (url) {
    console.log(url);
});
于 2015-05-05T23:48:57.137 回答