我们如何使用 FirefoxProfile 保留浏览器的旧会话?
How Can we Retain old session of browser using FirefoxProfile?
我在面试中被问到一个问题:
您正在处理一个会话,并且有一些针对初次用户的调查问卷。但是当用户关闭浏览器重新打开时,问卷肯定不会出现(因为他现在不是第一次使用)。我必须告诉我如何使用 TestNG 框架(使用 Java)在 Selenium 中自动化这个场景。所以基本上他想 close/quit 当前驱动程序并想使用另一个驱动程序重新打开页面并确保新会话记住用户之前访问过(就像手动完成的那样)。
我的回答是将 cookie 添加到新的驱动程序会话中。但他不想那样做。他想像用户一样进行测试。
他给了我关于为此使用 FirefoxProfile 以保留我们正在处理的配置文件的提示。
FirefoxProfile有什么用,在当前场景下如何使用。如果您能提供为此编写的代码,我将不胜感激。
因此归结为特定网站如何确定您是否已回答问卷。由于您没有在问题中给出任何解释,我只是假设它是按照大多数人的方式完成的 - 即 - 使用 cookie。
如果这个假设是正确的,那么你建议使用 cookie 是最好的建议,你的面试官说使用 FF profile 根本没有意义。我的意思是,我什至不确定你可以用 FF 配置文件做这样的事情,无论如何,在浏览器被触发时注入 cookie(或使用代理注入)绝对是一种解决这个问题的方法。这也可能是也可能不是因为你的面试官对网络的运作方式有一点扭曲的了解。所以:
He wanted to test exactly like a user would do.
在您的面试官心目中,这会是什么样子 - "a prankster user wondering around the web, closing his sessions whenever acceptable and starting again with clean installation of a browser, but in the same time always secretly injecting a cookie so he is not getting the questionnaire?"...我不是这方面的真正专家,也没有科学数据来证明这一点,但我几乎不认为这是我的用户所做的.. 所以用户真正做的是.. 什么都没有。是浏览器记住了 cookie,因此让 webapp 了解该用户是否访问过您。所以,是的,我再次声明,注入那个 cookie 是你应该做的。即使可以使用 FF 配置文件注入 cookie 或 smth - 只是拒绝这样做并使用 WebDriver
对象代替,因为使用配置文件执行此类任务毫无意义(启动 FF 时创建新的 FF 配置文件,还有声明:
"lose/quit the current driver and wanted to use another driver to
reopen the page and make sure that new session remembers that the user has visited before (just like its done manually).
"
:启动新的 WebDriver 实例和单击 "open firefox button as a user" 不是一回事。如果您不相信我,请查阅文档。调用 new FirefoxDriver()
会导致系统使用新的匿名配置文件加载新的 FF,而 "normal FF installation" 与他的所有缓存和其他内容一起存在于硬盘中。
所以有时在采访中他们故意给你一些毫无意义的问题,试图解决这些问题会伤害你的家人以及我的家人和整个人类 - 比如 "Write a regexp to parse HTML"。在这些情况下,您只需说:"Thats not really how we do it, let proceed to the next question"。在我看来,这就是你应该做的。祝你好运。
我在面试中被问到一个问题: 您正在处理一个会话,并且有一些针对初次用户的调查问卷。但是当用户关闭浏览器重新打开时,问卷肯定不会出现(因为他现在不是第一次使用)。我必须告诉我如何使用 TestNG 框架(使用 Java)在 Selenium 中自动化这个场景。所以基本上他想 close/quit 当前驱动程序并想使用另一个驱动程序重新打开页面并确保新会话记住用户之前访问过(就像手动完成的那样)。
我的回答是将 cookie 添加到新的驱动程序会话中。但他不想那样做。他想像用户一样进行测试。 他给了我关于为此使用 FirefoxProfile 以保留我们正在处理的配置文件的提示。
FirefoxProfile有什么用,在当前场景下如何使用。如果您能提供为此编写的代码,我将不胜感激。
因此归结为特定网站如何确定您是否已回答问卷。由于您没有在问题中给出任何解释,我只是假设它是按照大多数人的方式完成的 - 即 - 使用 cookie。
如果这个假设是正确的,那么你建议使用 cookie 是最好的建议,你的面试官说使用 FF profile 根本没有意义。我的意思是,我什至不确定你可以用 FF 配置文件做这样的事情,无论如何,在浏览器被触发时注入 cookie(或使用代理注入)绝对是一种解决这个问题的方法。这也可能是也可能不是因为你的面试官对网络的运作方式有一点扭曲的了解。所以:
He wanted to test exactly like a user would do.
在您的面试官心目中,这会是什么样子 - "a prankster user wondering around the web, closing his sessions whenever acceptable and starting again with clean installation of a browser, but in the same time always secretly injecting a cookie so he is not getting the questionnaire?"...我不是这方面的真正专家,也没有科学数据来证明这一点,但我几乎不认为这是我的用户所做的.. 所以用户真正做的是.. 什么都没有。是浏览器记住了 cookie,因此让 webapp 了解该用户是否访问过您。所以,是的,我再次声明,注入那个 cookie 是你应该做的。即使可以使用 FF 配置文件注入 cookie 或 smth - 只是拒绝这样做并使用 WebDriver
对象代替,因为使用配置文件执行此类任务毫无意义(启动 FF 时创建新的 FF 配置文件,还有声明:
"lose/quit the current driver and wanted to use another driver to reopen the page and make sure that new session remembers that the user has visited before (just like its done manually). "
:启动新的 WebDriver 实例和单击 "open firefox button as a user" 不是一回事。如果您不相信我,请查阅文档。调用 new FirefoxDriver()
会导致系统使用新的匿名配置文件加载新的 FF,而 "normal FF installation" 与他的所有缓存和其他内容一起存在于硬盘中。
所以有时在采访中他们故意给你一些毫无意义的问题,试图解决这些问题会伤害你的家人以及我的家人和整个人类 - 比如 "Write a regexp to parse HTML"。在这些情况下,您只需说:"Thats not really how we do it, let proceed to the next question"。在我看来,这就是你应该做的。祝你好运。