我是帶硒的java的新手。如何根據以下要求編寫更通用的代碼?如何在使用Java的硒中自動化
測試案例:搜索使用關鍵字醫生
- 轉到www.medinfi.com
- 選擇本地AECS,班加羅爾(地點輸入應該是動態的,也就是說,它應該是能夠改變的測試數據在任何時間)
- 在第二個搜索框中輸入搜索關鍵字「shai」以查找醫生/醫院名稱。 (檢索關鍵詞應當是動態的)
- 驗證在下拉的輸出與預期輸出數據集
包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();
}
}
看看教程https://www.airpair.com/selenium/posts/selenium-tutorial-with-java – bpjoshi