Selenium如何打开Web浏览器来运行selenium脚本

时间:2013-04-14 19:45:20

标签: selenium-webdriver testng

我从 selenium IDE 1.9.0 导出了一个脚本作为Java / TestNG / RemoteControl。

我想在 Eclipse 中使用 TestNG 运行此脚本,我希望看到该脚本在 Firefox 浏览器中播放。

我在线进行了一些搜索,但我无法使其正常运行。我需要一些关于如何使代码工作的指导和指导。非常感谢您的帮助。

这是我的代码:

import java.util.List;
import org.testng.annotations.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import static org.testng.Assert.*;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;

public class search_donor_suzy_ng //extends SeleneseTestNgHelper {
    @BeforeTest
    public void setUp() throws Exception {
        //initizlize Firefoxbrowser: 
        WebDriver ffoxdriver = new FirefoxDriver();
        String baseUrl = "www.google.com"; //sample URL
    }
    @Test 
    public void testSearch_donor_suzy_ng() throws Exception {
        // set overall speed of the test case
        selenium.setSpeed("4000");
        selenium.open("/?html=openid");
        selenium.click("css=input[type=\"submit\"]");
        selenium.waitForPageToLoad("30000");
        Thread.sleep(4000);
        selenium.type("id=edit-name", "jeffshu");
        selenium.type("id=edit-pass", "tEgvz9xsaNjnwe4Y");
        selenium.click("id=edit-submit");
        selenium.waitForPageToLoad("30000");
        Thread.sleep(4000);
        selenium.click("id=cmp_admin");
        selenium.waitForPageToLoad("30000");
        selenium.click("id=quicksearch_anchor");
        selenium.click("css=img[alt=\"Member\"]");
        selenium.waitForPageToLoad("30000");
        selenium.type("id=search_name", "suzy");
        selenium.click("css=input[type=\"image\"]");
        selenium.click("link=Balagia, Suzy");
        selenium.waitForPageToLoad("30000");
    }
    @AfterTest
    public void tearDown() throws Exception {
        ffoxdriver.quit();  
    }
} 

1 个答案:

答案 0 :(得分:0)

对于初学者,您需要参考文档@ http://docs.seleniumhq.org/docs/03_webdriver.jsp

在您的代码中,您正在使用您未使用的beforetest方法中的驱动程序对象。驱动程序是Webdriver启动浏览器的方式,而在您的测试方法中,您使用的是selenium和selenium 1命令。快速步骤,您可以用驱动程序替换您的selenium(将您的驱动程序声明放在类范围和方法范围内)。 SeleneseTestngHelper也是selenium1。 确保项目中有必要的webdriver jar。

webdriver中的某些命令与selenium 1不同,当您进行替换时,您可能会看到编译错误。查看驱动程序可用的方法。并使用相应的命令,例如。使用webdriver的get()而不是open()。您可以参考javadocs或使用您的ide来了解这些。