Selenium WebDriver上传文件/关闭资源管理器窗口

时间:2013-10-07 16:14:30

标签: java selenium selenium-webdriver

我在上传文件和关闭Windows资源管理器窗口时遇到了一些问题。测试运行正常,我没有收到任何错误,但我从未上传文件,上传弹出窗口永远不会关闭。

ApplyJob.java

package com.example.tests;

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.Select;

public class IJ_ApplyForJob {

 IJ_Login login = new IJ_Login();

@Test
public void SFJ_HomeSearchBoxNotLoggedIn(WebDriver driver)
{
    driver.findElement(By.id("kwdInput")).clear();
    driver.findElement(By.id("kwdInput")).sendKeys("Testing");
    //find the dropdown and search for the location dublin
    Select ddSelectLoc = new Select(driver.findElement(By.name("Location")));
    ddSelectLoc.selectByVisibleText("Dublin");
    //find the dropdown and search for the Category IT
    Select ddSelectCat = new Select(driver.findElement(By.id("home-category")));
    ddSelectCat.selectByVisibleText("IT");
    //click search button and search for term.
    driver.findElement(By.className("btn-search")).click();
    //click first job title
        driver.findElement(By.id("ctl00_ctl00_cphMain_cphMain_ucJobs_rptResult_ctl01_hlTitle")).click();
    //click apply for job
    driver.findElement(By.className("ApplyForJobButton")).click();
    //complete form
    driver.findElement(By.id("ctl00_ctl00_cphMain_cphMain_ucApplyJob_txtComments")).clear();
    driver.findElement(By.id("ctl00_ctl00_cphMain_cphMain_ucApplyJob_txtComments")).sendKeys("Cover Letter Testing");
    driver.findElement(By.id("ctl00_ctl00_cphMain_cphMain_ucApplyJob_txtFirstName")).clear();
    driver.findElement(By.id("ctl00_ctl00_cphMain_cphMain_ucApplyJob_txtFirstName")).sendKeys("Name");
    driver.findElement(By.id("ctl00_ctl00_cphMain_cphMain_ucApplyJob_txtSecondName")).clear();
    driver.findElement(By.id("ctl00_ctl00_cphMain_cphMain_ucApplyJob_txtSecondName")).sendKeys("Surname");
    driver.findElement(By.id("ctl00_ctl00_cphMain_cphMain_ucApplyJob_txtPhone")).clear();    
    driver.findElement(By.id("ctl00_ctl00_cphMain_cphMain_ucApplyJob_txtPhone")).sendKeys("12345678");
    driver.findElement(By.id("ctl00_ctl00_cphMain_cphMain_ucApplyJob_txtEmail")).clear();
    driver.findElement(By.id("ctl00_ctl00_cphMain_cphMain_ucApplyJob_txtEmail")).sendKeys("email@gmail.com");
   // Upload CV not logged in
    driver.findElement(By.id("ctl00_ctl00_cphMain_cphMain_ucApplyJob_fileCV")).sendKeys("C:\\Users\\Desktop\\CV_TEST.docx");
    driver.findElement(By.linkText("Open")).click();
    //submit application
    driver.findElement(By.id("ctl00_ctl00_cphMain_cphMain_ucApplyJob_btnSubmit")).click();
}
}

IJ_Test.java

package com.example.tests;

import org.junit.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;

public class IJ_test {
IJ_ApplyForJob afj = new IJ_ApplyForJob();
IJ_CommonSetup setup = new IJ_CommonSetup();
private WebDriver driver = new FirefoxDriver();

@Test
 public void runTest() throws Exception {
  setup.setUp(driver);
  setup.clearCookies(driver);
 afj.SFJ_HomeSearchBoxNotLoggedIn(driver);
  //afj.SFJ_HomeSearchBoxLoggedIn(driver);
  setup.tearDown();
}

}

我不确定我哪里出错了所以任何帮助都会受到赞赏。

1 个答案:

答案 0 :(得分:2)

问题是我从不等待上传的简历。 @smit的建议对我很有用。通过使用.implicitlyWait我能够暂停测试并等待CV上传。

Applyjob.java

package com.example.tests;

import java.util.concurrent.TimeUnit;

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.Select;

public class IJ_ApplyForJob {

 IJ_Login login = new IJ_Login();

@Test
public void SFJ_HomeSearchBoxNotLoggedIn(WebDriver driver)
{
    driver.findElement(By.id("kwdInput")).clear();
    driver.findElement(By.id("kwdInput")).sendKeys("Testing");
    //find the dropdown and search for the location dublin
    Select ddSelectLoc = new Select(driver.findElement(By.name("Location")));
    ddSelectLoc.selectByVisibleText("Dublin");
    //find the dropdown and search for the Category IT
    Select ddSelectCat = new Select(driver.findElement(By.id("home-category")));
    ddSelectCat.selectByVisibleText("IT");
    //click search button and search for term.
    driver.findElement(By.className("btn-search")).click();
    //click first job title
    driver.findElement(By.id("ctl00_ctl00_cphMain_cphMain_ucJobs_rptResult_ctl02_hlTitle")).click();
    //click apply for job
    driver.findElement(By.className("ApplyForJobButton")).click();
    //complete form
    driver.findElement(By.id("ctl00_ctl00_cphMain_cphMain_ucApplyJob_txtComments")).clear();
    driver.findElement(By.id("ctl00_ctl00_cphMain_cphMain_ucApplyJob_txtComments")).sendKeys("Cover Letter Testing");
    driver.findElement(By.id("ctl00_ctl00_cphMain_cphMain_ucApplyJob_txtFirstName")).clear();
    driver.findElement(By.id("ctl00_ctl00_cphMain_cphMain_ucApplyJob_txtFirstName")).sendKeys("Test");
    driver.findElement(By.id("ctl00_ctl00_cphMain_cphMain_ucApplyJob_txtSecondName")).clear();
    driver.findElement(By.id("ctl00_ctl00_cphMain_cphMain_ucApplyJob_txtSecondName")).sendKeys("Test");
    driver.findElement(By.id("ctl00_ctl00_cphMain_cphMain_ucApplyJob_txtPhone")).clear();    
    driver.findElement(By.id("ctl00_ctl00_cphMain_cphMain_ucApplyJob_txtPhone")).sendKeys("12345678");
       driver.findElement(By.id("ctl00_ctl00_cphMain_cphMain_ucApplyJob_txtEmail")).clear();
    driver.findElement(By.id("ctl00_ctl00_cphMain_cphMain_ucApplyJob_txtEmail")).sendKeys("craig.mccarthy@live.ie");
   // Upload CV not logged in
    driver.findElement(By.id("ctl00_ctl00_cphMain_cphMain_ucApplyJob_fileCV")).sendKeys("C:\\Users\\Craig\\Desktop\\CV_TEST.docx");
    //wait for CV to upload
    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
    //submit application
    driver.findElement(By.id("ctl00_ctl00_cphMain_cphMain_ucApplyJob_btnSubmit")).click();
}