如何使用selenium web驱动程序和groovy脚本在chrome中运行我的测试用例?

时间:2012-09-14 10:51:07

标签: selenium-webdriver

这是我的Firefox代码。如何为Chrome和IE自定义它?

可以让任何一个让我知道..我只使用groovy和selenium web驱动程序。由于Soap UI仅支持groovy,我只想在Groovy和Web驱动程序中执行此操作..

import org.openqa.selenium.By
import org.openqa.selenium.WebDriver
import org.openqa.selenium.WebElement
import org.openqa.selenium.firefox.FirefoxDriver
import org.openqa.selenium.support.ui.ExpectedCondition
import org.openqa.selenium.support.ui.WebDriverWait

         // Create a new instance of the Firefox driver
         WebDriver driver = new FirefoxDriver()

        // Extract URL for stdcheckout
       def link = context.expand( 'url' )

        // And now use this to visit stdcheckout
        driver.get(link)
        //driver.get("https://xxx-ft25.test.xxxx.eu/xxxx/login?partnerCode=xxx&paymentToken=4bbf8ee5-f102-424f-8a6f-28a8e26e0292")

        // Find the text input element by its name
        WebElement element = driver.findElement(By.name("email"))

        // Enter username
        element.sendKeys("1204@yahoo.com")


        //  Find the password element by its name
        WebElement element1 = driver.findElement(By.name("password"))

        // Enter password
        element1.sendKeys("asdfghjkl")

       // Click the Login button    
        driver.findElement(By.name("sbutton")).click();

       // Enter CVV2
       driver.findElement(By.id("cvv")).sendKeys("672");

      //WebElement element2 = driver.findElement(By.name("termsAndConditionsAccepted"))
      //element2.click();

      //click Paynow
      driver.findElement(By.cssSelector("input[type=\"button\"]")).click();


       log.info("Page title is: " + driver.getTitle())

        // Google's search is rendered dynamically with JavaScript.
        // Wait for the page to load, timeout after 10 seconds
        /*(new WebDriverWait(driver, 10)).until(new ExpectedCondition() {
            public Boolean apply(WebDriver d) {
             return d.getTitle().toLowerCase().startsWith("cheese!")
           }
        });*/

        // Should see: "cheese! - Google Search"
        //log.info("Page title is: " + driver.getTitle())

        //Close the browser
        driver.quit()

1 个答案:

答案 0 :(得分:1)

下载chrome驱动程序并将其放在任何位置,并在设置属性时给出路径....

package korporation.test.SampleTestGroup

import java.util.concurrent.TimeUnit

import org.openqa.selenium.By
import org.openqa.selenium.WebDriver
import org.openqa.selenium.chrome.ChromeDriver


    // set up the driver
    System.setProperty("webdriver.chrome.driver", "c:/chromedriver/chromedriver.exe")

    //Define the driver
    def WebDriver driver = new ChromeDriver()
    driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS)

     // And now use this to visit Google
    def link = context.expand( 'url'); 

     driver.get(link)

     def username = context.expand( '${#Project#username}' )

     // Find the text input element by its name
     driver.findElement(By.name("email")).sendKeys(username)



     //  Find the password element by its name
     driver.findElement(By.name("password")).sendKeys("asdfghjkl")

     // Click the Login button  
     driver.findElement(By.name("sbutton")).click();

    // Enter CVV2
    driver.findElement(By.id("cvv")).sendKeys("672");

    driver.findElement(By.name("termsAndConditionsAccepted")).click();

    driver.findElement(By.xpath("//*[@id=\"pay-now\"]")).click();


   //log.info("Page title is: " + driver.getTitle())

   assert (driver.getTitle()=="Mobile Phones | Contract Phones | Cheap Mobile Phone Deals & SIMs")
   //Close the browser
   driver.quit()