{"title":"Nightwatch 打开多个窗口","slug":"2018/01/open-multiple-windows","date":"2018-01-02T01:57:32.000Z","updated":"2020-07-03T08:49:34.493Z","comments":true,"path":"api/articles/2018/01/open-multiple-windows.json","excerpt":null,"covers":null,"content":"<p>如果想打开两个窗口并控制那个窗口怎么办？</p>\n<figure class=\"highlight javascript\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br><span class=\"line\">5</span><br><span class=\"line\">6</span><br><span class=\"line\">7</span><br><span class=\"line\">8</span><br><span class=\"line\">9</span><br><span class=\"line\">10</span><br><span class=\"line\">11</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"><span class=\"keyword\">var</span> url = process.env.BASE_URL, newWindow;</span><br><span class=\"line\"></span><br><span class=\"line\">client.execute(<span class=\"function\"><span class=\"keyword\">function</span> (<span class=\"params\">url, newWindow</span>) </span>&#123;</span><br><span class=\"line\">    <span class=\"built_in\">window</span>.open(url, newWindow, <span class=\"string\">'height=768,width=1024'</span>);</span><br><span class=\"line\">&#125;, [url, newWindow]);</span><br><span class=\"line\"></span><br><span class=\"line\">client.window_handles(<span class=\"function\"><span class=\"keyword\">function</span>(<span class=\"params\">result</span>) </span>&#123;</span><br><span class=\"line\">    <span class=\"keyword\">this</span>.verify.equal(result.value.length, <span class=\"number\">2</span>, <span class=\"string\">'There should be 2 windows open'</span>);</span><br><span class=\"line\">    newWindow = result.value[<span class=\"number\">1</span>];</span><br><span class=\"line\">    <span class=\"keyword\">this</span>.switchWindow(newWindow);</span><br><span class=\"line\">&#125;)</span><br></pre></td></tr></table></figure>\n","more":"<p>如果想打开两个窗口并控制那个窗口怎么办？</p>\n<figure class=\"highlight javascript\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br><span class=\"line\">5</span><br><span class=\"line\">6</span><br><span class=\"line\">7</span><br><span class=\"line\">8</span><br><span class=\"line\">9</span><br><span class=\"line\">10</span><br><span class=\"line\">11</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"><span class=\"keyword\">var</span> url = process.env.BASE_URL, newWindow;</span><br><span class=\"line\"></span><br><span class=\"line\">client.execute(<span class=\"function\"><span class=\"keyword\">function</span> (<span class=\"params\">url, newWindow</span>) </span>&#123;</span><br><span class=\"line\">    <span class=\"built_in\">window</span>.open(url, newWindow, <span class=\"string\">'height=768,width=1024'</span>);</span><br><span class=\"line\">&#125;, [url, newWindow]);</span><br><span class=\"line\"></span><br><span class=\"line\">client.window_handles(<span class=\"function\"><span class=\"keyword\">function</span>(<span class=\"params\">result</span>) </span>&#123;</span><br><span class=\"line\">    <span class=\"keyword\">this</span>.verify.equal(result.value.length, <span class=\"number\">2</span>, <span class=\"string\">'There should be 2 windows open'</span>);</span><br><span class=\"line\">    newWindow = result.value[<span class=\"number\">1</span>];</span><br><span class=\"line\">    <span class=\"keyword\">this</span>.switchWindow(newWindow);</span><br><span class=\"line\">&#125;)</span><br></pre></td></tr></table></figure>\n","categories":[{"name":"Automation","path":"api/categories/Automation.json"}],"tags":[{"name":"Nightwatch","path":"api/tags/Nightwatch.json"},{"name":"Automation","path":"api/tags/Automation.json"}]}