我是selenium webdriver的初学者,我试图通过阅读excel表来处理不同的定位器但是在这些中只需要一个数据就可以通过找到定位器“id”将它放在一个字段中,当它来到第二个文本时为此我们使用定位器“Xpath”,但它没有采取。所以,我的问题是我如何使用不同的定位器,如果可能的话不要使用switch case。 以下是我的代码:
public class MainClass {
private static final String BROWSER_PATH = "D:\\firefox.exe";
private static final String TEST_SUITE_PATH = "D:\\GmailTestSuite.xls";
private static final String OBJECT_REPOSITORY_PATH = "D:\\objectrepository.xls";
private static final String ADDRESS_TO_TEST = "https://www.gmail.com";
// other constants
private WebDriver driver;
private Properties properties;
/*private WebElement we;*/
public MainClass() {
File file = new File(BROWSER_PATH);
FirefoxBinary fb = new FirefoxBinary(file);
driver = new FirefoxDriver(fb, new FirefoxProfile());
driver.get(ADDRESS_TO_TEST);
}
public static void main(String[] args) throws IOException, BiffException {
MainClass main = new MainClass();
main.handleTestSuite();
}
private void handleTestSuite() throws BiffException, IOException {
ReadPropertyFile readConfigFile = new ReadPropertyFile();
properties = readConfigFile.loadPropertiess();
ExcelHandler testSuite = new ExcelHandler(TEST_SUITE_PATH, "Suite");
testSuite.columnData();
int rowCount = testSuite.rowCount();
System.out.println("Total Rows=" + rowCount);
for (int i = 1; i < rowCount; i++) {
String executable = testSuite.readCell(testSuite.getCell("Executable"), i);
System.out.println("Executable=" + executable);
if (executable.equalsIgnoreCase("y")) {
// exe. the process
String scenarioName = testSuite.readCell(testSuite.getCell("TestScenario"), i);
System.out.println("Scenario Name=" + scenarioName);
handleScenario(scenarioName);
}
}
}
private void handleScenario(String scenarioName) throws BiffException, IOException {
ExcelHandler testScenarios = new ExcelHandler(TEST_SUITE_PATH);
testScenarios.setSheetName("Login");
testScenarios.columnData();
int rowWorkBook1 = testScenarios.rowCount();
for (int j = 1; j < rowWorkBook1; j++) {
String framWork = testScenarios.readCell(testScenarios.getCell("FrameworkName"), j);
String operation = testScenarios.readCell(testScenarios.getCell("Operation"), j); // SendKey
String value = testScenarios.readCell(testScenarios.getCell("Value"), j);
System.out.println("FRMNameKK=" + framWork + ",Operation=" + operation +
",Value=" + value);
handleObjects(operation,value,framWork);
}
}
private void handleObjects(String operation,String value,String framWork) throws BiffException, IOException
{
System.out.println("HandleObject--> "+framWork);
ExcelHandler objectRepository = new ExcelHandler(OBJECT_REPOSITORY_PATH, "OR");
objectRepository.columnData();
int rowCount = objectRepository.rowCount();
System.out.println("Total Rows in hadleObject=" + rowCount);
for (int k = 1; k < rowCount; k++) {
String frameWorkName = objectRepository.readCell(objectRepository.getCell("FrameworkName"), k);
String ObjectName = objectRepository.readCell(objectRepository.getCell("ObjectName"), k);
String Locator = objectRepository.readCell(objectRepository.getCell("Locator"), k); // SendKey
System.out.println("FrameWorkNameV=" + frameWorkName +
",ObjectName=" + ObjectName + ",Locator=" + Locator);
if(framWork.equalsIgnoreCase(frameWorkName))
{
operateWebDriver(operation,Locator,value,ObjectName);
}
}
}
private void operateWebDriver(String operation,String Locator,String value, String objectName)
{
System.out.println("Operation execution in progress");
WebElement temp=getElement(Locator,objectName);
if (operation.equalsIgnoreCase("SendKey"))
{
temp.sendKeys(value);
}
if (operation.equalsIgnoreCase("Click"))
{
temp.click();
}
}
public WebElement getElement(String locator,String objectName)
{
WebElement temp = null;
if(locator.equalsIgnoreCase("id"))
{
temp = driver.findElement(By.id(objectName));
}else if(locator.equalsIgnoreCase("xpath")) {
temp = driver.findElement(By.xpath(objectName));
}
if(locator.equalsIgnoreCase("link"))
{
}
return temp;
}
}
答案 0 :(得分:0)
你需要反思。请参阅以下两种方法中的示例:
import java.lang.reflect.Method;
...
@Test
public void TestAmazon() {
driver = new ChromeDriver();
driver.navigate().to("http://www.amazon.com");
String locatorType = "id";
String locatorExpression = "twotabsearchtextbox";
By locator = createLocator(locatorType, locatorExpression);
WebElement textbox = driver.findElement(locator);
textbox.sendKeys("Star Wars: The Digital Movie Collection");
textbox.sendKeys(Keys.ENTER);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
locatorType = "className";
locatorExpression = "nav-logo-link";
locator = createLocator(locatorType, locatorExpression);
driver.findElement(locator).click();
}
private By createLocator(String locatorType, final String locatorExpression) {
By locator = null;
Class<By> byClass = By.class;
Class[] argTypes = new Class[] { String.class };
try {
Method m = byClass.getDeclaredMethod(locatorType, argTypes);
try {
locator = (By)m.invoke(null,locatorExpression);
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return locator;
}