[英]Unable to capture text inside new window in Java Selenium Headless browser
I'm running all my test suites on Jenkins which is deployed on AWS EC2 instance.我在部署在 AWS EC2 实例上的 Jenkins 上运行我的所有测试套件。 There is a scenario where when I click on a button, new small window opens up and I'm doing assertion for the text visible inside the small newly opened window.有一种情况是,当我单击一个按钮时,新的小 window 打开,我正在对新打开的小 window 中可见的文本进行断言。 But my tests are failing when I run using Headless mode.但是当我使用无头模式运行时,我的测试失败了。 But, same scripts works fine when I run scripts locally without opting for headless browser.但是,当我在本地运行脚本而不选择无头浏览器时,相同的脚本可以正常工作。
The issue here is the scripts are failing because of headless browser since it's unable to capture text inside small window which has opened after click of button.这里的问题是脚本由于无头浏览器而失败,因为它无法在单击按钮后打开的小 window 内捕获文本。 This class is extending InitiateDriver class which explained.这个 class 正在扩展解释的 InitiateDriver class。 Below class is trying to fetch text which is visible inside the new window which just opened after clicked on SignInWithGSuiteSSOClick() button. class 下面试图获取在新的 window 中可见的文本,该文本在单击 SignInWithGSuiteSSOClick() 按钮后刚刚打开。
Here is the code:
// click on a button
GSuiteobject.SignInWithGSuiteSSOClick().click();
String winHandleBefore = driver.getWindowHandle();
// Here trying to capture text inside new window opened up basically gmail window to enter email
String signInHeader = GSuiteobject.GsuiteSignInHeader().getText();
Assert.assertEquals(signInHeader, "Sign in");
GSuiteobject.GsuiteEmail().sendKeys("example@gmail.com");
InitiateDriver.java启动驱动程序.java
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
options.addArguments("--window-size=1920, 1080");
options.addArguments("--disable-gpu");
driver = new ChromeDriver(options);
The same code works in browser mode but not in headless.相同的代码在浏览器模式下有效,但在无头模式下无效。 But the driver will be initialized but it fails only while capturing text.但是驱动程序将被初始化,但仅在捕获文本时才会失败。 Please help me out I'm stuck here and unable to execute it on Jenkins as a headless browser.请帮帮我,我被困在这里,无法在 Jenkins 作为无头浏览器上执行它。
Headless chrome browser's gmail UI will be different from actual gmail UI(latest).无头 chrome 浏览器的 gmail UI 将不同于实际的 gmail UI(最新)。 Hence it fails when we run headless chrome for automating gmail login since xpaths will differ.因此,当我们运行无头 chrome 来自动登录 gmail 时,它会失败,因为 xpath 会有所不同。 We can validate that by taking Screenshot by running on headless and normal browser.我们可以通过在无头和普通浏览器上运行截屏来验证这一点。
I suggest to take screenshot using both headless and normal browser.我建议使用无头浏览器和普通浏览器截取屏幕截图。 To check on xpaths for headless we can try driver.getPageSource() method.要检查 xpaths 是否无头,我们可以尝试 driver.getPageSource() 方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.