JMeter的WebDriver的Javascript解释器无法访问第二个选项卡窗口

时间:2019-08-13 21:23:31

标签: selenium selenium-webdriver jmeter

我正在尝试使用以下代码运行WebDriver采样器:

var pkg = JavaImporter(org.openqa.selenium); //WebDriver classes
var support_ui = JavaImporter(org.openqa.selenium.support.ui.WebDriverWait); //WebDriver classes
var wait = new support_ui.WebDriverWait(WDS.browser, 5000);

WDS.sampleResult.sampleStart(); //captures sampler's start time
WDS.sampleResult.getLatency();
WDS.log.info("Sample started");

// Navigate to home
...

// Login
...

// Navigate to Messages
var messagesButton = WDS.browser.findElement(pkg.By.id('chat-button')); // saves the messages button into messagesButton
messagesButton.click(); // clicks the messages button which opens up link in a new tab
WDS.log.info("Clicked Messages Button");

var tabs = WDS.browser.getWindowHandles();
var tab = WDS.browser.getWindowHandle();

WDS.log.info("All Tabs: " + tabs);
WDS.log.info("Current Tab: " + tab);
WDS.log.info("Next Tab: " + tabs[tabs.size() - 1]);

WDS.browser.switchTo.window(tabs[tabs.size() - 1]);

// Load General Channel
var generalChannelButton = WDS.browser.findElement(pkg.By.linkText('general')); // saves the general channels button into generalChannelButton
messagesButton.click(); // clicks the messages button

WDS.sampleResult.sampleEnd();

在这种情况下,登录用户后,我指示他们单击消息按钮。当他们单击此按钮时,将打开一个新选项卡并运行自动OAuth流程。我希望测试执行的操作是导航到打开的新选项卡,然后等待OAuth完全处理并完成最终重定向。一旦发生这种情况,我想单击一个按钮。

但是,当我转到通过消息按钮打开的选项卡时,现在我陷入困境。这是打印出来的内容:

2019-08-13 15:17:15,480 INFO o.a.j.e.StandardJMeterEngine: Running the test!
2019-08-13 15:17:15,484 INFO o.a.j.s.SampleEvent: List of sample_variables: []
2019-08-13 15:17:15,488 INFO o.a.j.g.u.JMeterMenuBar: setRunning(true, *local*)
2019-08-13 15:17:15,834 INFO o.a.j.e.StandardJMeterEngine: Starting ThreadGroup: 1 : Thread Group
2019-08-13 15:17:15,834 INFO o.a.j.e.StandardJMeterEngine: Starting 1 threads for group Thread Group.
2019-08-13 15:17:15,834 INFO o.a.j.e.StandardJMeterEngine: Thread will continue on error
2019-08-13 15:17:15,834 INFO o.a.j.t.ThreadGroup: Starting thread group... number=1 threads=1 ramp-up=1 delayedStart=false
2019-08-13 15:17:15,836 INFO o.a.j.t.ThreadGroup: Started thread group number 1
2019-08-13 15:17:15,837 INFO o.a.j.e.StandardJMeterEngine: All thread groups have been started
2019-08-13 15:17:15,839 INFO o.a.j.t.JMeterThread: Thread started: Thread Group 1-1
2019-08-13 15:17:17,755 INFO c.g.j.p.w.s.WebDriverSampler: Sample started
2019-08-13 15:17:25,007 INFO c.g.j.p.w.s.WebDriverSampler: Sample ended - navigated to yt-portal.raccoongang.com
2019-08-13 15:17:25,180 INFO c.g.j.p.w.s.WebDriverSampler: Entered Username
2019-08-13 15:17:25,285 INFO c.g.j.p.w.s.WebDriverSampler: Entered Password
2019-08-13 15:17:42,159 INFO c.g.j.p.w.s.WebDriverSampler: Submitted Login
2019-08-13 15:17:42,303 INFO c.g.j.p.w.s.WebDriverSampler: Clicked Messages Button
2019-08-13 15:17:42,314 INFO c.g.j.p.w.s.WebDriverSampler: All Tabs: [CDwindow-2071965AEF3647D3F81E3C45E722803F, CDwindow-680DEFAD39924A4A2FE9BF6C6CCF907F]
2019-08-13 15:17:42,315 INFO c.g.j.p.w.s.WebDriverSampler: Current Tab: CDwindow-2071965AEF3647D3F81E3C45E722803F
2019-08-13 15:17:42,316 INFO c.g.j.p.w.s.WebDriverSampler: Next Tab: null
2019-08-13 15:17:42,317 ERROR c.g.j.p.w.s.WebDriverSampler: TypeError: WDS.browser.switchTo.window is not a function in <eval> at line number 39
2019-08-13 15:17:42,318 INFO o.a.j.t.JMeterThread: Thread is done: Thread Group 1-1
2019-08-13 15:17:42,318 INFO o.a.j.t.JMeterThread: Thread finished: Thread Group 1-1
2019-08-13 15:17:42,543 INFO o.a.j.e.StandardJMeterEngine: Notifying test listeners of end of test
2019-08-13 15:17:42,544 INFO o.a.j.g.u.JMeterMenuBar: setRunning(false, *local*)

如您所见,All Tabs打印带有两个选项卡的数组,但是当我尝试访问索引1处的选项卡时,它打印出null ...

如何继续访问第二个选项卡并等待其加载特定的重定向URL,然后继续?

2 个答案:

答案 0 :(得分:0)

更仔细地阅读jmeter.log文件:


    <template>
        <label class="wrapper" :data-error="validation && errors.first(name)" :class="{ error: errors && errors.first(name), label }">
            <span class="label" v-if="label">{{label}}</span>
            <div class="input" :class="{
                'input--search': type === 'search'
            }">
                <input
                    v-if="validation"
                    :aria-label="ariaLabel"
                    v-shortkey.focus="shortKey"
                    :disabled="disabled"
                    :type="type"
                    :class="classes"
                    :value="value"
                    :name="name"
                    :placeholder="placeholder"
                    @search="reset"
                    @input="update"
                    v-validate="validation"
                    :autocomplete="autocomplete"
                />
                <input
                    v-else
                    :aria-label="ariaLabel"
                    v-shortkey.focus="shortKey"
                    :disabled="disabled"
                    :type="type"
                    :class="classes"
                    :value="value"
                    :name="name"
                    :placeholder="placeholder"
                    @search="reset"
                    @input="update"
                    :autocomplete="autocomplete"
                />
                <Icon class="icon" v-if="iconName && !value" :name="iconName" />
            </div>
        </label>
    </template>

我的期望是您需要替换此行:


    <template>
        <form class="form" v-on:submit.prevent>
            <slot />
            <Input
                v-if="isLabel && displayLabel"
                class="label"
                ref="label"
                type="text"
                :label="$t('subscriptions.new.label-placeholder')"
                name="label"
                :validation="{ required: true }"
                :value="label"
                @input="onLabelChange"
            />
            <hr />
            <Terms
                v-if="!termsAccepted"
                :value="terms"
                @input="onTermsToggle"
            />
            <Button
                class="submit button-next"
                submit
                :disabled="nextDisabled"
                @click="next"
            >
                {{ $t('subscriptions.new.next') }}
            </Button>
        </form>
    </template>

与此:

TypeError: WDS.browser.switchTo.window is not a function in <eval> at line number 39

因为switchTo()是一个函数,因此它需要带尾括号。

更多信息:The WebDriver Sampler: Your Top 10 Questions Answered

答案 1 :(得分:0)

结果是,即使您打印出结果的getWindowHandles(),“ javascript”解释器也不会将Array解释为Set,而是解释为typeofgetWindowHandles()中的object

因此,您要做的是从Iterator中创建一个Set,然后以比Javascript类型更类似于Java的方式对其进行迭代。

var tabs = WDS.browser.getWindowHandles();
var tabsIterator = tabs.iterator();
var tabsArr = [];
while(tabsIterator.hasNext()) {
    tabsArr.push(tabsIterator.next());
}
var chatTab = tabsArr[tabsArr.length - 1];
WDS.browser.switchTo().window(chatTab);
WDS.log.info("Navigated to Chat Tab");