GVKun编程网logo

FluentWait类型不是通用的;不能使用参数对其进行参数化 Selenium和Java导致FluentWait类错误

14

关于FluentWait类型不是通用的;不能使用参数对其进行参数化Selenium和Java导致FluentWait类错误的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于.NetWebAp

关于FluentWait类型不是通用的;不能使用参数对其进行参数化 Selenium和Java导致FluentWait类错误的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于.Net Web Api中利用FluentValidate进行参数验证的方法、appiumlibrary 用的是 1.2.3 没有 Wait Until Page Contains Element 这个关键字。、c# – .NET Selenium NoSuchElementException; WebDriverWait.Until()不起作用、c# – Selenium Wait不等待Element可点击等相关知识的信息别忘了在本站进行查找喔。

本文目录一览:

FluentWait类型不是通用的;不能使用参数对其进行参数化 Selenium和Java导致FluentWait类错误

FluentWait类型不是通用的;不能使用参数对其进行参数化 Selenium和Java导致FluentWait类错误

我正在与Selenium Standalone Server 3.0.1。我正在尝试向ExplicitWait代码中添加,以在元素变为可见时通过xpath检测元素。为了获得一些Java帮助,我寻找了源代码, Selenium StandaloneServer 3.0.1但找不到它。我在selenium-java-2.53.1发行版中找到了源代码。我下载并找到selenium-java-2.53.1-srcs并添加到我的EclipseIDE。在的帮助下FluentWait,我只需将代码复制粘贴到我的代码中,Eclipse IDE然后更改变量名。

文档中的示例代码如下:

   // Waiting 30 seconds for an element to be present on the page, checking   // for its presence once every 5 seconds.    Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)       .withTimeout(30, SECONDS)       .pollingEvery(5, SECONDS)       .ignoring(NoSuchElementException.class);    WebElement foo = wait.until(new Function<WebDriver, WebElement>() {     public WebElement apply(WebDriver driver) {       return driver.findElement(By.id("foo"));      }    });

但是,当我实现此代码时,只需复制粘贴即可:

       Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)           .withTimeout(30, TimeUnit.SECONDS)           .pollingEvery(5, TimeUnit.SECONDS)           .ignoring(NoSuchElementException.class);       WebElement element = wait.until(new Function<WebDriver, WebElement>()        {         public WebElement apply(WebDriver driver) {           return driver.findElement(By.xpath("//p[text()=''WebDriver'']"));         }       });

我在FluentWaitClass 上遇到错误The type FluentWait is not generic; it cannot beparameterized with arguments <WebDriver>

这是我的进口清单:

    import java.util.concurrent.TimeUnit;    import org.apache.log4j.Logger;    import org.apache.log4j.PropertyConfigurator;    import org.openqa.selenium.By;    import org.openqa.selenium.NoSuchElementException;    import org.openqa.selenium.WebDriver;    import org.openqa.selenium.WebElement;    import org.openqa.selenium.chrome.ChromeDriver;    import org.openqa.selenium.support.ui.Wait;    import com.google.common.base.Function;

有人可以帮我吗?


更新资料

增加了一个回答关于的修改构造 FluentWaitseleniumV3.11.0

答案1

小编典典

您需要在等待中指定期望的条件,以下是修改后的代码,可以解决您的问题。

码:

import java.util.concurrent.TimeUnit;import org.junit.Test;import org.openqa.selenium.By;import org.openqa.selenium.NoSuchElementException;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.support.ui.ExpectedConditions;import org.openqa.selenium.support.ui.FluentWait;import org.openqa.selenium.support.ui.Wait;public class DummyClass{    WebDriver driver;    @Test    public void test()    {        Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)            .withTimeout(30, TimeUnit.SECONDS)            .pollingEvery(5, TimeUnit.SECONDS)            .ignoring(NoSuchElementException.class);        until(new Function<WebElement, Boolean>()         {            public Boolean apply(WebElement element)            {                return element.getText().endsWith("04");            }            private void until(Function<WebElement, Boolean> function)            {                driver.findElement(By.linkText("Sample Post2"));            }        }    }}

.Net Web Api中利用FluentValidate进行参数验证的方法

.Net Web Api中利用FluentValidate进行参数验证的方法

前言

本文主要介绍了关于.Net Web Api用FluentValidate参数验证的相关内容,下面话不多说了,来一起看看详细的介绍吧。

方法如下

安装FluentValidate

在ASP.NET Web Api中请安装 FluentValidation.WebApi版本

创建一个需要验证的Model

 public class Product 
 {
  public string name { get; set; }
  public string des { get; set; }
  public string place { get; set; }
 }

配置FluentValidation,需要继承AbstractValidator类,并添加对应的验证规则

 public class ProductValidator : AbstractValidator<Product>
 {
  public ProductValidator()
  {
   RuleFor(product => product.name).NotNull().NotEmpty();//name 字段不能为null,也不能为空字符串
  }
 }

在Config中配置 FluentValidation

在 WebApiConfig配置文件中添加

public static class WebApiConfig
{
 public static void Register(HttpConfiguration config)
 {
  // Web API routes
  ...
  FluentValidationModelValidatorProvider.Configure(config);
 }
}

验证参数

需要在进入Controller之前进行验证,如果有错误就返回,不再进入Controller,需要使用 ActionFilterAttribute

public class ValidateModelStateFilter : ActionFilterAttribute
{
 public override void OnActionExecuting(HttpActionContext actionContext)
 {
  if (!actionContext.ModelState.IsValid)
  {
   actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.BadRequest, actionContext.ModelState);
  }
 }
}

如果要让这个过滤器对所有的Controller都起作用,请在WebApiConfig中注册

public static class WebApiConfig
{
 public static void Register(HttpConfiguration config)
 {
  // Web API configuration and services
  config.Filters.Add(new ValidateModelStateFilter());

  // Web API routes
  ...

FluentValidationModelValidatorProvider.Configure(config);
 }
}

如果指对某一个Controller起作用,可以在Controller注册

[ValidateModelStateFilter]
public class ProductController : ApiController
{
 //具体的逻辑
}

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对的支持。

您可能感兴趣的文章:
  • 详解ASP.NET Core WebApi 返回统一格式参数
  • asp.net core webapi项目配置全局路由的方法示例
  • ASP.NET WebAPI连接数据库的方法
  • .Net WebApi消息拦截器之MessageHandler的示例
  • .Net Core2.1 WebAPI新增Swagger插件详解
  • ASP.net WebAPI跨域调用问题的解决方法
  • asp.net core webapi 服务端配置跨域的实例
  • ASP.NET Core 2.0 WebApi全局配置及日志实例
  • asp.net core 2.0 webapi集成signalr(实例讲解)
  • 详解.net core webapi 前后端开发分离后的配置和部署

appiumlibrary 用的是 1.2.3 没有 Wait Until Page Contains Element 这个关键字。

appiumlibrary 用的是 1.2.3 没有 Wait Until Page Contains Element 这个关键字。

appiumlibrary 用的是 1.2.3 没有 Wait Until Page Contains Element 这个关键字。
pip list 查看本地 appiumblirary 版本
对比了 api 上的版本和本地的版本,发现本地版本好久,api 上是 1.3.6
运行 pip install --upgrade robotframework-appiumlibrary。
安装后重启 ride,此时已能识别 Wait Until Page Contains Element 关键字。。重现跑脚本。
结果又报 monitorcolors option not recognised
运行 pip install --upgrade robotframework-ride
更新 ride 成功后,重启 ride 再运行脚本。
又报 keyword ''Capture Page Screenshot'' could not be run on failure: WebDriverException: Message
更新 appium, npm update -g appium
更新 appium 至 1.5.3 版本,各种重启,再运行脚本,脚本终于跑成功了。。。。
 
KEYWORD AppiumLibrary . Open Application  http://localhost:4723/wd/hub, platformName=Android, platformVersion=4.2.2, deviceName=0123456789ABCDEF, appPackage=com.UCMobile, appActivity=com.uc.browser.InnerUCMobile
 
Documentation:

Opens a new application to given Appium server.

Start / End / Elapsed: 20160826 18:00:16.289 / 20160826 18:00:20.318 / 00:00:04.029
00:00:00.008KEYWORD AppiumLibrary . Capture Page Screenshot
 
Documentation:

Takes a screenshot of the current page and embeds it into the log.

Start / End / Elapsed: 20160826 18:00:20.297 / 20160826 18:00:20.305 / 00:00:00.008
18:00:20.304 FAIL No application is open  
18:00:18.292 INFO Could not get IP address for host: localhost  
18:00:20.306 WARN Keyword ''Capture Page Screenshot'' could not be run on failure: No application is open  
18:00:20.318 FAIL URLError: <urlopen error [Errno 10061] >

c# – .NET Selenium NoSuchElementException; WebDriverWait.Until()不起作用

c# – .NET Selenium NoSuchElementException; WebDriverWait.Until()不起作用

我们有一个.NET 4.5,MVC,C#项目.我们正在使用Selenium进行UI测试,测试会在我们有wait.Until()的行上间歇性地失败.一个这样的例子是:

NoSuchElementException was unhandled by user code
An exception of type 'OpenQA.Selenium.NoSuchElementException' occurred in WebDriver.dll but was not handled in user code
Additional information: Unable to locate element: {"method":"css selector","selector":"#assessment-472 .status-PushedToSEAS"}

它就在这里抛出:

Thread.Sleep(600);
wait.Until(drv => drv.FindElement(By.CssSelector("#" + assessmentQueueId + " .status-PushedToSEAS")));

我可以看到浏览器打开,我看到它到达那一点,我可以检查元素以查看元素是否存在.它的id完全正确.

我们有这个问题很多,到目前为止解决方案是在它前面抛出Thread.Sleep(600)(或类似的时间). wait.Until()的重点是不必这样做,这使我们的测试套件变得非常长.另外,正如您在上面的示例中所看到的,有时我们甚至在将Thread.Sleep()放在它前面之后也会遇到问题,我们必须延长时间.

为什么.NET Selenium的WebDriver.Until()不起作用,并且有一种不同的方式来做同样的事情而不需要等待一段时间?同样,问题是间歇性的,这意味着它有时只会发生,它可能发生在任何数量的wait.Until()语句中,而不仅仅是显示的那个!

编辑:

这是一个类变量.
私人WebDriver等等;

它实例化如下:

this.wait = new webdriverwait(driver,TimeSpan.FromSeconds(10));

解决方法

我决定不使用webdriverwait.Until(),而是在主驱动程序上使用隐式等待:

driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));

为了给我这个想法,我在一个不同的问题上充分赞扬了this answer.

c# – Selenium Wait不等待Element可点击

c# – Selenium Wait不等待Element可点击

我有一个动态加载的页面,包含一个按钮.我正在尝试等待按钮可以使用C#绑定点击selenium.我有以下代码:

webdriverwait wait = new webdriverwait(Driver.Instance,TimeSpan.FromSeconds(30));
        wait.Until(ExpectedConditions.ElementToBeClickable(By.Id("addinspectionButton")));

        var button = Driver.Instance.FindElement(By.Id("addinspectionButton"));
        button.Click();

这不起作用. click事件永远不会被触发. selenium脚本不会抛出异常,提示ID为“addinspectionButton”的元素不存在.它只是无法点击它.如果我添加一个Thread.Sleep(3000)在wait语句和我得到按钮元素句柄的行之间它可以工作.

我在这里没有正确使用ExpectedConditions.ElementToBeClickable吗?

解决方法

事实证明,在将按钮动态添加到页面后,事件被绑定到按钮.所以按钮被点击但没有发生任何事情.放置在代码中的睡眠线程只是给客户端事件时间绑定.

我的解决方案是单击按钮,检查预期结果,然后重复,如果预期结果尚未在DOM中.

由于预期的结果是打开一个表单,我像这样轮询DOM:

button.Click();//click button to make form open
        var forms = Driver.Instance.FindElements(By.Id("inspectionDetailsForm"));//query the DOM for the form
        var times = 0;//keep tabs on how many times button has been clicked
        while(forms.Count < 1 && times < 100)//if the form hasn't loaded yet reclick the button and check for the form in the DOM,only try 100 times
        {

            button.Click();//reclick the button
            forms = Driver.Instance.FindElements(By.Id("inspectionDetailsForm"));//requery the DOM for the form
            times++;// keep track of times clicked
        }

关于FluentWait类型不是通用的;不能使用参数对其进行参数化 Selenium和Java导致FluentWait类错误的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于.Net Web Api中利用FluentValidate进行参数验证的方法、appiumlibrary 用的是 1.2.3 没有 Wait Until Page Contains Element 这个关键字。、c# – .NET Selenium NoSuchElementException; WebDriverWait.Until()不起作用、c# – Selenium Wait不等待Element可点击等相关知识的信息别忘了在本站进行查找喔。

本文标签: