登录后保持会话-硒-javascript [英] keep session after login - selenium - javascript

查看:84
本文介绍了登录后保持会话-硒-javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Selenium Web驱动程序和node js自动执行几个页面.我可以登录,但是登录后我想使用由Web驱动程序启动的同一会话,以便可以在受会话保护的页面上进行自动测试.这是我的尝试

I am trying to automate couple of pages using selenium web driver and node js . I was able to login , but after login I want to use same session initiated by web driver so that I can do automated testing on session protected page. This is my attempt

async function login(){
    Let  d =  await new Builder()
                        .forBrowser('chrome')
                        .build();
    await d.get('https://demo.textdomain.com/')
    await d.findElement(By.id('username')).sendKeys('admin ')
    await d.findElement(By.id('password')).sendKeys('admin');
    await d.findElement(By.css('button[type="submit"]')).click();
    d.getPageSource().then(function(content) {

            if(content.indexOf('Welcome text') !==-1 ) {
             console.log('Test passed');
             console.log('landing page');
             d.get('https://demo.textdomain.com/landingpage') //this is still going to login page as i cannot use the previous session 
            } else {
                console.log('Test failed');
                return false;
            }
            //driver.quit();
        });

}

login();

我是否在登录后不小心丢弃了浏览器.

推荐答案

您可能正在处理计时问题.硒很快移动.比您作为用户进行交互的方式要快得多.因此,它通常以似乎无法预测的方式起作用.但这仅仅是因为Selenium的运行速度比您作为用户的速度快得多.为了解决此问题,您应该充分利用Selenium的内置driver.wait.例如:

You might just be dealing with timing issues. Selenium moves very fast. Way faster than you can interact as a user. So it often acts in what seems like unpredictable ways. But that's only because Selenium is acting much faster than you would as a user. In order to work around this, you should make good use of Selenium's built-in driver.wait. For example:

const button = driver.wait(
  until.elementLocated(By.id('my-button')), 
  20000
);

button.click();

以上内容一直等待,直到ID为my-button的按钮出现在DOM中,然后单击它.最多等待20000毫秒,但是只要按钮可用,操作就会结束.

The above waits until the button with id my-button is present in the DOM, and then clicks it. It will wait for a maximum of 20000 milliseconds, but will finish as soon as the button becomes available.

因此,在您的情况下,如果在用户成功登录后还有其他可用的内容,则可以等待该元素,然后再转到代码中的新页面.

So in your case, if there is something that becomes available after the user is successfully logged in, you could wait on that element before going to the new page in your code.

顺便说一句,我也不确定为什么要使用getPageSource()?这似乎是获得所需内容的一种非常笨拙的方法.那不是您可以获取其内容的元素中的内容吗?

As an aside, I'm also not so sure why you are using getPageSource()? That seems like a very heavy-handed way to get what you are looking for. Isn't that content inside an element you could get the contents of?

我写了一篇有关如何使用Selenium和Node.js编写可靠的浏览器测试,这可能有助于您更详细地了解本文中的上述代码示例,以及可以用来可靠地等待各种操作的其他技术.浏览器中的条件.

I wrote an article about How to write reliable browser tests using Selenium and Node.js which might help you understand in more detail the code example above from the article, along with other techniques you can use to wait reliably for a variety of conditions in the browser.

这篇关于登录后保持会话-硒-javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆