简体   繁体   English

使用sendKeys上传文件硒

[英]File upload selenium using sendKeys

My situation is that I am automating testing file upload feature. 我的情况是我正在自动化测试文件上传功能。 Now the automation ci works on another machine/box and the browser is opened in another machine(s) for the automation testing. 现在,自动化ci在另一台机器/盒子上工作,并且在另一台机器上打开浏览器以进行自动化测试。 I am able to package (add the test input file to be uploaded) in the jar. 我可以在jar中打包(添加要上传的测试输入文件)。 But the jar is in another another machine as mentioned above and the browser is in another machine(s). 但是,如上所述,该jar位于另一台计算机中,而浏览器位于另一台计算机中。 Since the browser machine is not fixed and picked up at automation runtime, so how can I have the input file which I need to upload available on the machine where browser is running. 由于浏览器机器不是固定的,而是在自动化运行时获取的,因此如何在运行浏览器的机器上获取需要上传的输入文件。

I tried to copy the file after extracting it from the jar, but obviously it doesn't get copied in the browser machine(s), from where it is uploaded. 从jar提取文件后,我尝试复制文件,但是显然文件没有从上载位置的浏览器中复制。

Is it even possible to have that file available in the browser machine(s)? 甚至有可能在浏览器机器上提供该文件吗?

One thing you can do is putting the uploading file in to a shared folder which can be accessed from all the running machines. 您可以做的一件事是将上传文件放入一个共享文件夹,该文件夹可以从所有运行的计算机访问。 And give the file location from the shared folder. 并从共享文件夹中指定文件位置。 You can give the sendKeys command to the upload field like below 您可以将sendKeys命令提供给上载字段,如下所示

upload_textfield.sendKeys("\\shared_Folder\upload.txt")

static WebDriver driver; 静态WebDriver驱动程序; public static void main(String[] args) throws InterruptedException, FindFailed { 公共静态void main(String [] args)抛出InterruptedException,FindFailed {

    System.setProperty("webdriver.gecko.driver", "E:\\doftware\\geckodriver-v0.10.0-win64\\geckodriver.exe");
    driver =new FirefoxDriver();
    driver.get("https://accounts.google.com/ServiceLogin?service=mail&passive=true&rm=false&continue=https://mail.google.com/mail/&ss=1&scc=1&ltmpl=default&ltmplcache=2&emr=1&osid=1");

    driver.findElement(By.id("Email")).sendKeys("emailaddress");
    driver.findElement(By.id("next")).click();
    Thread.sleep(500);
    driver.findElement(By.id("Passwd")).sendKeys("Password");
    driver.findElement(By.id("signIn")).click();
    Thread.sleep(5000);
    driver.findElement(By.xpath("//div[@class='T-I J-J5-Ji T-I-KE L3']")).click();
    Thread.sleep(500);
    driver.findElement(By.xpath("//div[@class='a1 aaA aMZ']")).click();

    org.sikuli.script.Pattern open= new org.sikuli.script.Pattern("C:\\Users\\narendra\\Desktop\\test\\filename.PNG");
    org.sikuli.script.Pattern open1= new org.sikuli.script.Pattern("C:\\Users\\narendra\\Desktop\\test\\open.PNG");

    org.sikuli.script.Screen scr= new org.sikuli.script.Screen();
    scr.setAutoWaitTimeout(30);
    scr.type(open, "C:\\Users\\narendra\\Desktop\\test\\searchButton");
    scr.click(open1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM