2017-07-14 41 views
-4

我是帶硒的java的新手。如何根據以下要求編寫更通用的代碼?如何在使用Java的硒中自動化

測試案例:搜索使用關鍵字醫生

  1. 轉到www.medinfi.com
  2. 選擇本地AECS,班加羅爾(地點輸入應該是動態的,也就是說,它應該是能夠改變的測試數據在任何時間)
  3. 在第二個搜索框中輸入搜索關鍵字「shai」以查找醫生/醫院名稱。 (檢索關鍵詞應當是動態的)
  4. 驗證在下拉的輸出與預期輸出數據集

包com.medinfi.code;

import java.util.concurrent.TimeUnit; 
import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.chrome.ChromeDriver; 
import org.testng.annotations.AfterClass; 
import org.testng.annotations.BeforeClass; 
import org.testng.annotations.Test; 

    public class MedinfiChallenge { 

    private WebDriver driver; 
    @BeforeClass(alwaysRun = true) 
    public void setUp() throws Exception 
    { 
    System.setProperty("webdriver.chrome.driver", "F:\\SeleniumProject\\Medinfi\\Driver\\chromedriver.exe"); 
    driver = new ChromeDriver(); 
    driver.get("http://www.medinfi.com/"); 
    driver.manage().window().maximize(); 
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 

    } 

    @Test 
    public void testMedinfi() throws Exception 
    { 
     driver.findElement(By.xpath("//*[@id='city-locality1']")).sendKeys("AECS Layout, Bengaluru"); 
     driver.findElement(By.xpath("//*[@id='ip1_text1']")).sendKeys("Hospitals"); 
     driver.findElement(By.xpath("//*[@id=\"searchIcon\"]")).click(); 

    } 

    @AfterClass(alwaysRun = true) 
    public void tearDown() throws Exception 
    { 
     driver.quit(); 
    } 

    } 
+2

看看教程https://www.airpair.com/selenium/posts/selenium-tutorial-with-java – bpjoshi

回答

-1

爲了使您的代碼更通用的,可以用於自動化測試,必須把所有的設置代碼到一個文件中(通常使用在現實世界中的Excel文件),並從該文件中讀取。這些信息包括:驅動程序路徑,鏈接到web需要測試,xpath ...這些都是通用信息會頻繁更改,因爲您未測試1個網站或1個鏈接,您需要測試多個網站,每個網站都有特定的xpath測試。有一個框架供你參考Open2Test你可以得到源代碼並學習它們的工作方式。

+0

感謝您的回覆。你可以請你看看我的代碼,如果有什麼不對的地方,建議我。 – User01

+0

你原來的問題是如何通用你的代碼,我已經提供給你的方式。上面的代碼似乎沒有錯,但它只是作爲演示版本,因爲所有的東西都是硬代碼。 –