【问题标题】:Best way to add query param to all navigations将查询参数添加到所有导航的最佳方法
【发布时间】:2021-11-16 12:59:27
【问题描述】:

我正在寻找一种解决方案,在使用 WebDriverIO 导航后在每个 URL 上添加查询参数。

例如: 基本网址:https://www.google.com/?block=true

当我单击从上述 URL 加载的页面上的按钮时,加载的新 URL 是 https://www.google.com/search-page

我想将?block=true 附加到所有导航。

对于基本 URL,我可以使用方法browser.url("https://www.google.com/?block=true")。不知道如何添加到使用点击操作导航的其他页面。

【问题讨论】:

    标签: javascript node.js url


    【解决方案1】:

    您可以使用URLSearchParams 生成复杂的搜索参数。看看下面的例子。

    const baseUrl = "https://www.google.com/?block=true"; // window.location.href
    const searchUrl = "https://www.google.com/search?test=testparam";
    
    function getRedirectUrl(baseUrl, newUrl) {
      let oldParms = new URL(baseUrl).searchParams;
      const newUrlParams = new URL(newUrl).searchParams;
      for(let [value, key] of oldParms.entries()){
        newUrlParams.append(key, value);
      }
      const newSearch = new URL(newUrl).search;
      return `${newUrl.slice(0, -1 * newSearch.length)}?${newUrlParams.toString()}`;
    }
    
    console.log(getRedirectUrl(baseUrl, searchUrl));

    【讨论】:

      猜你喜欢
      • 2011-04-11
      • 2013-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-06
      • 2015-01-08
      • 1970-01-01
      相关资源
      最近更新 更多