如何在Java中使用Selenium lib时指定要模拟的浏览器?

时间:2016-01-29 06:51:29

标签: java internet-explorer selenium htmlunit-driver

我的网页仅适用于IE,因为它在javascript中使用ActiveXObject。 在对内部工具进行编码以测试此网页时,如何指定浏览器类型和版本?

Java代码是:



[Serializable]
    [XmlRoot("projects")]
    public class BulkProjectRoot
    {
        public BulkProjectRoot()
        {
            BulkProjectDetails = new List<BulkProjectData>();
        }

        [XmlArray("project")]
        [XmlArrayItem("details")]
        public List<BulkProjectData> BulkProjectDetails { get; set; }
    }

 [Serializable]
    [XmlRoot("details")]

    public class BulkProjectData
    {
        public BulkProjectData()
        {
            ProjectResearches = new List<ProjectResearchOutputs>();
        }

        [XmlElement("projectName")]
        public string Name { get; set; }

        [XmlElement("uniqueID")]
        public string ProjectUniqueId { get; set; }

        [XmlElement("researchOutputList")]
        public List<ProjectResearchOutputs> ProjectResearches { get; set; }

    }

[Serializable]
[XmlRoot("researchOutputList")]
public class ProjectResearchOutputs
{
    public ProjectResearchOutputs()
    {
        ResearchItemsList = new List<ResearchOutputItems>();
    }

    [XmlElement("item")]
    public List<ResearchOutputItems> ResearchItemsList { get; set; }

}

   [Serializable]
    [XmlRoot("item")]

    public class ResearchOutputItems
    {

        [XmlElement("pubDate")]
        public DateTime? PublishDate { get; set; }

        [XmlElement("title")]
        public string ResearchTitle { get; set; }

        [XmlElement("link")]
        public string ResearchLink { get; set; }

        [XmlElement("guid")]
        public string ResearchGuid { get; set; }

        [XmlElement("description")]
        public string ResearchDescription { get; set; }

    }
&#13;
&#13;
&#13;

网页是:

&#13;
&#13;
package main; 

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;

public class Temp  {
    public static void main(String[] args) {
        // Create a new instance of the html unit driver
        // Notice that the remainder of the code relies on the interface, 
        // not the implementation.
        WebDriver driver = new HtmlUnitDriver();

        // And now use this to visit my web page
        driver.get("http://www.test.com:8000");
        
        // Find the text input element by its id
        driver.findElement(By.id("test")).click();
        driver.manage().timeouts().implicitlyWait(300,TimeUnit.SECONDS); 

        // Check the title of the page
        System.out.println("Result is: " + driver.findElement(By.id("demo")).getText());
        driver.quit();
    }
}
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

您需要使用InternetExplorerDriver代替HtmlUnitDriver。查看this page了解详细信息。

答案 1 :(得分:0)

这基本上是在网格级别完成的,您需要设置网格并再次运行测试。

看看http://www.seleniumhq.org/docs/07_selenium_grid.jsp 并搜索&#34;多个版本的浏览器&#34;。

HTH