GVKun编程网logo

一种使用Java Robot API和Java Selenium WebDriver的文件上传解决方案(java robotframework)

19

本文将分享一种使用JavaRobotAPI和JavaSeleniumWebDriver的文件上传解决方案的详细内容,并且还将对javarobotframework进行详尽解释,此外,我们还将为大家带来

本文将分享一种使用Java Robot API和Java Selenium WebDriver的文件上传解决方案的详细内容,并且还将对java robotframework进行详尽解释,此外,我们还将为大家带来关于JavaSelenium Webdriver:修改navigator.webdriver标志以防止selenium检测、node.js – 带有webdriverjs的javascript或带有selenium-webdriver的java?、selenium java api中关于webdriverwait的问题、Selenium Webdriver java 积累的相关知识,希望对你有所帮助。

本文目录一览:

一种使用Java Robot API和Java Selenium WebDriver的文件上传解决方案(java robotframework)

一种使用Java Robot API和Java Selenium WebDriver的文件上传解决方案(java robotframework)

我看到很多人在使用Selenium
WebDriver的测试环境中上传文件时遇到问题。我使用了硒WebDriver和Java,并且遇到了同样的问题。我终于找到了解决方案,因此我将其发布在这里,希望对其他人有所帮助。

当我需要在测试中上传文件时,请单击按钮中的Webdriver,然后等待窗口“打开”弹出。然后,我将路径复制到剪贴板中的文件,然后将其粘贴到“打开”窗口中,然后单击“输入”。之所以起作用,是因为当窗口“打开”弹出时,焦点始终位于“打开”按钮中。

您将需要以下类和方法:

import java.awt.Robot;import java.awt.event.KeyEvent;import java.awt.Toolkit;import java.awt.datatransfer.StringSelection;public static void setClipboardData(String string) {   StringSelection stringSelection = new StringSelection(string);   Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);}

这就是我在打开“打开”窗口后所做的事情:

setClipboardData("C:\\path to file\\example.jpg");//native key strokes for CTRL, V and ENTER keysRobot robot = new Robot();robot.keyPress(KeyEvent.VK_CONTROL);robot.keyPress(KeyEvent.VK_V);robot.keyRelease(KeyEvent.VK_V);robot.keyRelease(KeyEvent.VK_CONTROL);robot.keyPress(KeyEvent.VK_ENTER);robot.keyRelease(KeyEvent.VK_ENTER);

就是这样。它对我有用,我希望对某些人有用。

答案1

小编典典

实际上,也有一种内置的技术。它应该可以在所有浏览器和操作系统中使用。

Selenium 2(WebDriver)Java示例:

// assuming driver is a healthy WebDriver instanceWebElement fileInput = driver.findElement(By.xpath("//input[@type=''file'']"));fileInput.sendKeys("C:/path/to/file.jpg");

这个想法是将文件的绝对路径直接发送到一个元素,您通常会单击该元素以获取模式窗口-即<input type=''file'' />element。

JavaSelenium Webdriver:修改navigator.webdriver标志以防止selenium检测

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/")

node.js – 带有webdriverjs的javascript或带有selenium-webdriver的java?

node.js – 带有webdriverjs的javascript或带有selenium-webdriver的java?

我们的团队正计划开发一个框架来自动化手动测试用例.但我们仍然坚持在 javascript和java之间进行选择.通过一些搜索,我们发现Webdriverjs是javascript的selenium绑定.现在,主要障碍是我们是否应该使用selenium-webdrier来使用webdriverjs或java的javascript?

我们已经使用selenium-webdriver了解java,但是当我们开始使用新框架时,我们只想打开简单,快速和可靠的选项.

请建议javascript与webdriverjs(我们需要学习javascript)或java与selenium-webdriver?

解决方法

我们遇到了您现在看到的同样挑战.
Java堆栈上有很多selenium工程师,但在Node.js和selenium-webdriver堆栈上并不多.

大多数较新的现代测试框架都是在Node.js中完成的,因为前端和后端都是使用NodeJS完成的,并将继续这种趋势.我要问的问题是开发团队是使用Java(例如Tomcat)还是使用Node.js来开发他们的产品.如果他们使用的是Node.js,那么将测试框架用同一种语言编写也是理想的.

这是为了方便

>开发和自动化团队,定位器等之间的协作.
>减轻开发人员编写硒测试的摩擦

marcel Erz在我们的South Bay Selenium聚会上对此进行了非常好的介绍.我强烈建议您在做出决定之前先仔细阅读.

Java与JavaScript(用于UI测试)

>前端工程师编写的大多数测试
>不熟悉Java及其生态系统
>上下文切换
>不太可能接受测试

http://www.marcelerz.com/blog/talk-nodejs-based-selenium-testing-south-bay-selenium-meetup

现在,如果您开始使用Javascript,那么主要的挑战当然是异步.大多数自动化工程师习惯于使用Python和Java同步模式.需要一些时间来习惯javascript的异步行为.但最终结果是值得的.

我们的框架主要用Node.js编写,我们使用Mocha作为我们的线束和测试运行器.推荐的断言库是Chai,但如果您需要特殊需求,可以使用其他库.

我们选择的selenium库是nd上的WebDriverJs(区分大小写)又名selenium-webdriver,它是官方的JavaScript端口.我们使用selenium-webdriver的主要原因之一是代码可读性以及实现类似同步语法的能力,以便让Java测试工程师牢记.这可以通过利用内置的Promise Manager控制流和Mocha Test Wrapper来实现,它可以自动处理对promise管理器的所有调用,使代码非常同步.

https://code.google.com/p/selenium/wiki/WebDriverJs#Writing_Tests

然后,这是一个添加自己的框架和构建页面对象的问题. Javascript中的页面对象是一个全新的野兽,您必须很好地掌握原型以及如何模拟Java的继承.

您还应该在npm中使用selenium-standalone作为本地和远程执行的唯一通信点,而不是在测试中创建驱动程序实例(本地/远程).这样,框架具有相同的接口并使事情保持一致.您不希望跟踪多个本地驱动程序可执行文件并更新它们.一个包装可以处理所有事情.

如果您已经阅读过这里,并且您非常确定您将使用Node.js路由而不是Java.下面是我们框架的一个非常简化的版本,可以帮助您入门.它具有上面描述的所有实现.欢迎任何拉动请求!

https://github.com/mekdev/mocha-selenium-pageobject

selenium java api中关于webdriverwait的问题

selenium java api中关于webdriverwait的问题

如何解决selenium java api中关于webdriverwait的问题?

我刚刚安装了 selenium jar 文件,我从网上复制了一个小程序来测试是否一切正常。我遇到了这个问题,互联网没有给出具体的解决方案。以下程序是我复制的。

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.webdriverwait;
import static org.openqa.selenium.support.ui.ExpectedConditions.presenceOfElementLocated;
import java.time.Duration;

public class sample {

    public static void main(String args[]) {
        WebDriver driver = new ChromeDriver();
        webdriverwait wait = new webdriverwait(driver,Duration.ofSeconds(10));
        try {
            driver.get("https://google.com/ncr");
            driver.findElement(By.name("q")).sendKeys("cheese" + Keys.ENTER);
            WebElement firstResult = wait.until(presenceOfElementLocated(By.cssSelector("//a/h3")));
            System.out.println(firstResult.getAttribute("textContent"));
        } finally {
            driver.quit();
        }
    }
}

即使我导入了正确的文件,我也收到一条错误消息,指出“构造函数 webdriverwait(webdriver duration) 未定义”。我是硒的新手。 感谢您抽出宝贵时间。

解决方法

(或一个)问题是 Duration.ofSeconds() 返回一个类型为 Duration 的对象,而 WebDriverWait 期望在该参数中接收一个 long。简单尝试

WebDriverWait wait = new WebDriverWait(driver,10);

Selenium Webdriver java 积累

Selenium Webdriver java 积累

OSC 请你来轰趴啦!1028 苏州源创会,一起寻宝 AI 时代
Selenium Webdriver java 积累 博客分类: java

基于 maven 的 Selniun 2.0 webdriver 环境快速搭建
http://www.cnblogs.com/fnng/archive/2011/12/02/2272610.html

Selenium Webdriver 学习: http://jarvi.iteye.com/category/203994

https://github.com/easonhan007/webdriver_guide

Selenium WebDriver 经验杂记
http://blog.csdn.net/ant_yan/article/details/8185899
http://blog.csdn.net/aerchi/article/category/936247

在 Eclipse 中使用 JUnit4 进行单元测试

  • 在 Eclipse 中使用 JUnit4 进行单元测试(初级篇)
  • 在 Eclipse 中使用 JUnit4 进行单元测试(中级篇)
  • 在 Eclipse 中使用 JUnit4 进行单元测试(高级篇)


1. Selenium Webdriver java 最大化浏览器窗口。

System.setProperty("webdriver.firefox.bin","D:/MozillaFirefox/firefox.exe");
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();

参考文章:http://xebee.xebia.in/2012/04/06/maximize-browser-window-in-selenium-web-driver/

2. Selenium Webdriver java 浏览器中前进,后退,刷新操作。

System.setProperty("webdriver.firefox.bin","D:/MozillaFirefox/firefox.exe");
WebDriver driver = newFirefoxDriver();
driver.manage().window().maximize();
driver.get("http://www.google.com.hk");
driver.navigate().forward();// 前进
driver.navigate().back();// 后退
driver.navigate().refresh();// 刷新
 
3.WebDriver 的几个实现:
Name ofdriver                Available on whichOS?       Classto instantiate 
HtmlUnit Driver              All                      org.openqa.selenium.htmlunit.HtmlUnitDriver 
Firefox Driver                 All                      org.openqa.selenium.firefox.FirefoxDriver 
Internet Explorer Driver   Windows          org.openqa.selenium.ie.InternetExplorerDriver 
Chrome Driver               All                     org.openqa.selenium.chrome.ChromeDriver 
 
(1)HtmlUnitDriver 是纯 java 的驱动器,全部在内存中运行,不会实际打开浏览器,但其速度很快。 
(2)Firefox Driver:速度慢。  
4. TestNG 实例:
http://blog.csdn.net/yuxinlong2006/article/details/6765522
http://magustest.com/blog/automationtesting/webdriver-testng/
WebDriver+TestNG+ANT 实现多浏览器兼容性测试
http://www.51testing.com/?uid-375957-action-viewspace-itemid-817942
5. Webdriver 针对 iframe 中的元素定位
Selenium2 在使用 get () 方法打开一个网页的时候,是不会继续加载里面的 iframe 中的内容的 (这一点与 Selenium 有所区别)。那么,我们就需要人为的要求 Selenium2 对 iframe 中的内容进行加载。
例子一:
driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@g-editor-iframe'']"))); 
例子二:
driver.switchTo().frame(driver.findElement(By.id("baiduSpFrame")));
切换主页
用 getWindowHandle () 方法可以快速的进行切换回主页:
StringstrMainHandler = driver.getWindowHandle();
driver.switchTo().window(strMainHandler);
关于 iframe 中 元素定位和 xpath, cssSelector 定位可参考:
http://blog.csdn.net/dancedan/article/details/7406942
6.Webdriver 中操作下拉列表
对下拉框进行操作时首先要定位到这个下拉框,new 一个 Selcet 对象,然后对它进行操作。
参见 http://jarvi.iteye.com/blog/1450883
7.Webdriver 利用 Actions 类模拟鼠标和键盘的操作
参考一: http://jarvi.iteye.com/blog/1468690
参考二: http://www.51testing.com/?uid-368273-action-viewspace-itemid-842910
8. Hudson + WebDriver 组织自动化测试
参见: http://www.51testing.com/?uid-350678-action-viewspace-itemid-814225
 
9. Webdirver 利用 actions 实现 mouseover 方法
Actionsbuilder =new Actions(driver);
Actionsmousehover=builder.moveToElement(driver.findElement(By. xxx("***") ));
mousehover.perform(); 
红色部分根据实际情况修改。
10. webdriver 中 层级定位
参见: http://jarvi.iteye.com/blog/1448025
11. webdriver 执行 js 脚本
http://jarvi.iteye.com/blog/1447755
12. webdriver 等待页面加载完成
http://jarvi.iteye.com/blog/1453662
13. 获取页面对象的属性值
绿色为对象的属性
pk1=wd.findElement(By.xpath("//input[@id=''public_key'']")).getAttribute("value");
System.out.println(pk1);
14. Webdriver 通过调用 JavascriptExecutor 使对象隐藏对象出现,操作对象。
http://blog.sina.com.cn/s/blog_539a70d30101ajsg.html
15.Selenium 中 webdriver 的 quit()和 close()区别
在关闭 driver 时用 Driver.Quit (); 不要用 Driver.Close ();
Driver.Quit() Quit this dirver, closing everyassociated windows;
Driver.Close() Close the current window, quiting thebrowser if it is the last window currently open.
参考: http://blog.sina.com.cn/s/blog_6997f015010161xe.html
 
转载 http://blog.csdn.net/achang21/article/details/11702843

今天关于一种使用Java Robot API和Java Selenium WebDriver的文件上传解决方案java robotframework的分享就到这里,希望大家有所收获,若想了解更多关于JavaSelenium Webdriver:修改navigator.webdriver标志以防止selenium检测、node.js – 带有webdriverjs的javascript或带有selenium-webdriver的java?、selenium java api中关于webdriverwait的问题、Selenium Webdriver java 积累等相关知识,可以在本站进行查询。

本文标签: