2014-11-25 55 views
1

我希望打印結帳過程中出現的運費,運費是根據地址動態計算的,或者可能是購物車中的產品。我想在計算後打印第二張結帳頁面上的運費。使用硒如何在結帳頁面上打印運費

import com.thoughtworks.selenium.*; 
import org.junit.After; 
import org.junit.Before; 
import org.junit.Test; 
import static org.junit.Assert.*; 
import java.util.regex.Pattern; 

public class testing { 
    private Selenium selenium; 

@Before 
public void setUp() throws Exception { 
    selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://testing-671.myshopify.com/"); 
    selenium.start(); 
} 

@Test 
public void testtesting() throws Exception { 
    selenium.open("/"); 
    selenium.click("link=Testing"); 
    selenium.waitForPageToLoad("30000"); 
    selenium.click("//a[contains(text(),'Catalog')]"); 
    selenium.waitForPageToLoad("30000"); 
    selenium.click("css=img[alt=\"testing\"]"); 
    selenium.waitForPageToLoad("30000"); 
    selenium.click("id=add-to-cart"); 
    selenium.waitForPageToLoad("30000"); 
    selenium.click("id=checkout"); 
    selenium.waitForPageToLoad("30000"); 
    selenium.type("id=checkout_email", "[email protected]"); 
    selenium.type("id=checkout_shipping_address_first_name", "test"); 
    selenium.type("id=checkout_shipping_address_last_name", "test"); 
    selenium.type("id=checkout_shipping_address_company", "test"); 
    selenium.type("id=checkout_shipping_address_address1", "test"); 
    selenium.type("id=checkout_shipping_address_address2", "test"); 
    selenium.type("id=checkout_shipping_address_city", "test"); 
    selenium.select("id=checkout_shipping_address_country", "label=Albania"); 
    selenium.click("css=option[value=\"Albania\"]"); 
    selenium.select("id=checkout_shipping_address_country", "label=India"); 
    selenium.select("id=checkout_shipping_address_province", "label=Chandigarh"); 
    selenium.type("id=checkout_shipping_address_zip", "160062"); 
    selenium.click("name=commit"); 
    selenium.waitForPageToLoad("30000"); 
} 

@After 
public void tearDown() throws Exception { 
    selenium.stop(); 
} 
} 

回答

1

這是我在硒IDE中運行和導出測試用例的程序硒RC和JUnit檢索更新後的代碼:

import com.thoughtworks.selenium.*; 
import org.junit.After; 
import org.junit.Before; 
import org.junit.Test; 
import static org.junit.Assert.*; 
import java.util.regex.Pattern; 

public class Testcases { 
    private Selenium selenium; 

    @Before 
    public void setUp() throws Exception { 
     selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://testing-671.myshopify.com"); 
     selenium.start(); 
    } 

    @Test 
    public void testCases() throws Exception { 
     selenium.open("/"); 
     selenium.click("link=Testing"); 
     selenium.waitForPageToLoad(""); 
     selenium.click("//a[contains(text(),'Catalog')]"); 
     selenium.waitForPageToLoad("30000"); 
     selenium.click("css=img[alt=\"testing\"]"); 
     selenium.waitForPageToLoad("30000"); 
     selenium.click("id=add-to-cart"); 
     selenium.waitForPageToLoad("30000"); 
     selenium.click("id=checkout"); 
     selenium.waitForPageToLoad("30000"); 
     selenium.type("id=checkout_email", "[email protected]"); 
     selenium.type("id=checkout_shipping_address_first_name", "test"); 
     selenium.type("id=checkout_shipping_address_last_name", "test"); 
     selenium.type("id=checkout_shipping_address_company", "test"); 
     selenium.type("id=checkout_shipping_address_address1", "test"); 
     selenium.type("id=checkout_shipping_address_address2", "test"); 
     selenium.type("id=checkout_shipping_address_city", "test"); 
     selenium.select("id=checkout_shipping_address_country", "label=Albania"); 
     selenium.select("id=checkout_shipping_address_province", "label=Chandigarh"); 
     selenium.type("id=checkout_shipping_address_zip", "160062"); 
     selenium.click("name=commit"); 
     selenium.waitForPageToLoad("30000"); 
     for (int second = 0;; second++) { 
      if (second >= 60) fail("timeout"); 
      try { if (selenium.isElementPresent("//span[@class='total-line__name' and contains(text(),'Shipping')]/following-sibling::strong[contains(text(),'Rs.')]")) break; } catch (Exception e) {} 
      Thread.sleep(1000); 
     } 

     String Price = selenium.getText("//span[@class='total-line__name' and contains(text(),'Shipping')]/following-sibling::strong"); 
     System.out.println(Price); 
    } 

    @After 
    public void tearDown() throws Exception { 
     selenium.stop(); 
    } 
} 
+1

Subh感謝這個偉大的幫助。 – Manish 2014-11-25 12:53:33

+0

很高興它解決了.. :)請標記爲答案也。謝謝.. :) – Subh 2014-11-25 13:38:59

+0

嘿@subh stackoverflow.com/questions/27644839/請回答我這個問題 – Manish 2014-12-26 07:33:33