对于使用Java在SeleniumWebDriver中使用PageObjects,PageFactory和WebDriverWait感兴趣的读者,本文将会是一篇不错的选择,我们将详细介绍javasel
对于使用Java在Selenium WebDriver中使用PageObjects,Page Factory和WebDriverWait感兴趣的读者,本文将会是一篇不错的选择,我们将详细介绍java selenium select,并为您提供关于java – Selenium WebDriver中selenium.waitForPageToLoad(“30000”)的等效代码是什么?、java – Wait.until()与Webdriver PageFactory元素、JavaSelenium Webdriver:修改navigator.webdriver标志以防止selenium检测、jquery-selectors – Selenium WebDriver PageFactory使用jQuery Selector FindsBy?的有用信息。
本文目录一览:- 使用Java在Selenium WebDriver中使用PageObjects,Page Factory和WebDriverWait(java selenium select)
- java – Selenium WebDriver中selenium.waitForPageToLoad(“30000”)的等效代码是什么?
- java – Wait.until()与Webdriver PageFactory元素
- JavaSelenium Webdriver:修改navigator.webdriver标志以防止selenium检测
- jquery-selectors – Selenium WebDriver PageFactory使用jQuery Selector FindsBy?
使用Java在Selenium WebDriver中使用PageObjects,Page Factory和WebDriverWait(java selenium select)
我一直在使用Selenium WebDriver为我曾经处理过的某些项目实施功能测试。我正在尝试将Page Object设计模式与Page
Factory一起使用来排除我的定位器。我还创建了一个静态WaitTool对象(单例),该对象通过可选的超时参数实现了几种等待技术。
我当前的问题是在PageFactory尝试初始化WebElement之前,我想使用我的等待方法。我要等待的原因是因为PageFactory可能会尝试在页面元素在页面上可用之前初始化页面元素。
这是一个示例PageObject:
public class SignInPage extends PageBase {
@FindBy(id = "username")
@CacheLookup
private WebElement usernameField;
@FindBy(id = "password")
@CacheLookup
private WebElement passwordField;
@FindBy(name = "submit")
@CacheLookup
private WebElement signInButton;
public SignInPage(WebDriver driver) {
super(driver);
WaitTool.waitForPageToLoad(driver,this);
// I'd like initialisation to occur here
}
public MainPage signInWithValidCredentials(String username,String password){
return signIn(username,password,MainPage.class);
}
private <T>T signIn(String username,String password,Class<T> expectedPage) {
usernameField.type(username);
passwordField.type(password);
signInButton.click();
return PageFactory.initElements(driver,expectedPage);
}
}
这是一个示例TestObject:
public class SignInTest extends TestBase {
@Test
public void SignInWithValidCredentialsTest() {
SignInPage signInPage = PageFactory.initElements(driver,SignInPage.class);
MainPage mainPage = signInPage.signInWithValidCredentials("sbrown","sbrown");
assertThat(mainPage.getTitle(),is(equalTo(driver.getTitle())));
}
}
我倾向于将逻辑尽可能地放在Page Object中(包括等待),因为它使测试用例更具可读性。
java – Selenium WebDriver中selenium.waitForPageToLoad(“30000”)的等效代码是什么?
selenium.waitForPagetoLoad("30000");
Selenium WebDriver中的等效java代码是什么?
解决方法
>如果你需要等待60秒,你可以使用Thread.sleep(60000)
>如果您想确保页面已加载(可能小于或大于60秒),我建议使用以下方法:
确定目标网页中的元素&等待它可以点击.然后您确定该页面已加载.
webdriverwait wait = new webdriverwait(driver,120); wait.until(ExpectedConditions.elementToBeClickable(By.id(id)));
WebDriver等待最多120秒.对于可点击的元素.如果元素在此之前可单击,则测试将进行.
java – Wait.until()与Webdriver PageFactory元素
@FindBy(xpath = "//textarea") public InputBox authorField;
请帮忙.我希望使用带有注释元素的wait(ExpectedConditions).像这样:
wait.until(visibilityOfElementLocated(authorField));
代替:
wait.until(visibilityOfElementLocated(By.xpath("//textarea")));
谢谢你提前
解决方法
ExpectedConditions.visibilityOf(authorField);
查看任何预期条件的源代码.编写自己的条件非常容易,可以做你想做的一切.
JavaSelenium Webdriver:修改navigator.webdriver标志以防止selenium检测
我正在尝试使用selenium和铬在网站中自动化一个非常基本的任务,但是以某种方式网站会检测到铬是由selenium驱动的,并阻止每个请求。我怀疑该网站是否依赖像这样的公开DOM变量https://stackoverflow.com/a/41904453/648236来检测selenium驱动的浏览器。
我的问题是,有没有办法使navigator.webdriver标志为假?我愿意尝试修改后重新尝试编译selenium源,但是似乎无法在存储库中的任何地方找到NavigatorAutomationInformation源https://github.com/SeleniumHQ/selenium
任何帮助深表感谢
PS:我还从https://w3c.github.io/webdriver/#interface尝试了以下操作
Object.defineProperty(navigator, ''webdriver'', { get: () => false, });
但是它仅在初始页面加载后更新属性。我认为网站会在执行脚本之前检测到变量。
答案1
小编典典从当前的实现开始,一种理想的访问网页而不被检测到的方法是使用ChromeOptions()该类向以下参数添加几个参数:
排除enable-automation
开关的集合
关掉 useAutomationExtension
通过以下实例ChromeOptions
:
Java示例:
System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");ChromeOptions options = new ChromeOptions();options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));options.setExperimentalOption("useAutomationExtension", false);WebDriver driver = new ChromeDriver(options);driver.get("https://www.google.com/");
Python范例
from selenium import webdriveroptions = webdriver.ChromeOptions()options.add_experimental_option("excludeSwitches", ["enable-automation"])options.add_experimental_option(''useAutomationExtension'', False)driver = webdriver.Chrome(options=options, executable_path=r''C:\path\to\chromedriver.exe'')driver.get("https://www.google.com/")
jquery-selectors – Selenium WebDriver PageFactory使用jQuery Selector FindsBy?
说我有一个登录页面.
public class LoginPage { [FindsBy(How = How.Id,Using = "SomeReallyLongIdBecauSEOfAspNetControlsAndPanels_username"] public IWebElement UsernameField { get; set; } [FindsBy(How = How.Id,Using = "SomeReallyLongIdBecauSEOfAspNetControlsAndPanels_password"] public IWebElement PasswordField { get; set; } [FindsBy(How = How.Id,Using = "submitButtonId")] public IWebElement SubmitButton { get; set; } private readonly IWebDriver driver; public LoginPage(IWebDriver driver) { this.driver = driver; if(!driver.Url.Contains("Login.aspx")) { throw new NotFoundException("This is not the login page."); } PageFactory.InitElements(driver,this); } public HomePage Login(Credentials cred) { UsernameField.sendKeys(cred.Username); PasswordField.SendKeys(cred.Password); SubmitButton.Click(); return new HomePage(driver); } } [TestFixture] public class Test : TestBase { private IWebDriver driver; [SetUp] public void SetUp() { driver = StartDriver(); // some function which returns my driver in a wrapped event or something so I can log everything it does. } [Test] public void test() { new LoginPage(driver) .Login(new Credentials { Username = "username",Password = "password" }) .someHomePageFunction() }
最后,我知道页面配置会发生变化,id会大致保持不变,但是我的项目的情况正在迅速改变.我知道xPath是另一种选择,但是由于页面是如何基于某些标准生成的,所以这仍然会变得很痛苦,因为路径并不总是相同的.
使用上面的当前代码,页面被加载,PageFactory init是通过Page Constructor的元素.太棒了这就是我目前使用的.
目前,如果某些事情并非总是在页面上生成,直到某一步骤.我通常会做以下事情:
private const string ThisIsTheUserNameFieldId = "usernamefield";
然后使用以下命令启动webdriver:
// Navigate to login page // code here // Enter in credentials driver.FindElement(By.Id(ThisIsTheUserNameFieldId)).SendKeys(cred.Username);
不像PageFactory那样结构良好,但它肯定是我无法解决的要求.
我最近遇到了一些与C#.Net一起使用的jQuery Selector代码,它扩展了RemoteWebDriver的功能,我可以使用jQuery选择器在页面上查找我的Elements.
Selenium jQuery for C#.Net (Including Source)
// So I can do things like this: driver.FindElement(By.jQuery("a").Find(":contains('Home')").Next())
有谁知道如何扩展Selenium WebDriver中的[FindsBy]属性,以便可以使用类似下面的内容(伪代码)?
[FindsBy(How = How.jQuery,Using = "div[id$='txtUserName']")] public IWebElement UsernameField { get; set; }
解决方法
var driver = new FirefoxDriver { Url = "http://www.google.com" }; var element = (IWebElement)((IJavaScriptExecutor)driver).ExecuteScript("return document.getElementsByName('q')[0];"); element.SendKeys("hello world");
您可以通过首先注入jquery(取自JQuerify并修改)来轻松扩展它以允许jquery选择器:
const string js = @"{var b=document.getElementsByTagName('body')[0]; if(typeof jQuery=='undefined'){var script=document" + @".createElement('script'); script.src='http://code.jquery.com/jquery-latest.min.js';var head=document" + @".getElementsByTagName('head')[0],done=false;script.onload=script.onreadystatechange=function(){if(!" + @"done&&(!this.readyState||this.readyState=='loaded'||this.readyState=='complete')){done=true;script." + @"onload=script.onreadystatechange=null;head.removeChild(script);}};head.appendChild(script);}}"; ((IJavaScriptExecutor)driver).ExecuteScript(js);
然后运行javascript来选择你想要的元素:
var driver = new FirefoxDriver { Url = "http://www.google.com" }; var element = (IWebElement)((IJavaScriptExecutor)driver).ExecuteScript(@"return $('input[name*=""q""]')[0];"); element.SendKeys("hello world");
我们今天的关于使用Java在Selenium WebDriver中使用PageObjects,Page Factory和WebDriverWait和java selenium select的分享已经告一段落,感谢您的关注,如果您想了解更多关于java – Selenium WebDriver中selenium.waitForPageToLoad(“30000”)的等效代码是什么?、java – Wait.until()与Webdriver PageFactory元素、JavaSelenium Webdriver:修改navigator.webdriver标志以防止selenium检测、jquery-selectors – Selenium WebDriver PageFactory使用jQuery Selector FindsBy?的相关信息,请在本站查询。
本文标签: