私はC#で物事を行うことについて話すことはできませんが、Javaは十分に類似しているため、以下の例から、必要なことを実行する方法を理解できるはずです。
私は次のようなブラウザクラスを持っています:
public class Browser
{
public WebDriver getDriver(String driverName)
{
WebDriver driver = null;
if(driverName == "firefox")
{
driver = new FirefoxDriver();
}
else if(driverName == "chrome")
{
File chromeFile = new File ("C:/webdrivers/chromedriver.exe");
System.setProperty("webdriver.chrome.driver", chromeFile.getAbsolutePath());
driver = new ChromeDriver();
}
else if(driverName == "ie")
{
File ieFile = new File("C:/webdrivers/IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", ieFile.getAbsolutePath());
driver = new InternetExplorerDriver();
}
else if(driverName == "iOS")
{
try
{
driver = new RemoteWebDriver(new URL("http://localhost:3001/wd/hub"), DesiredCapabilities.ipad());
} catch (MalformedURLException e)
{
e.printStackTrace();
}
}
return driver;
}
}
次に、ブラウザクラスを呼び出すメソッドが1つあります。次に、テストメソッドによって、使用するブラウザーが決定されるため、次のようになります。
public class EditMedications_Cancel
{
Browser browser = new Browser();
Random rand = new Random();
HighlightElement highlight = new HighlightElement();
WebDriver driver;
private void cancelButton(String driverName)
{
int min = 1; //minimum random row
int max = 5; //maximum random row
driver = browser.getDriver(driverName); //get the Browser
int rowNum = (rand.nextInt(max - min + 1) + min); //Random row
JavascriptExecutor js = (JavascriptExecutor)driver;
//Open Browser, navigate to Patient Facing
driver.manage().window().maximize();
driver.get("http://someIP.com");
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
//Get a random row from the table and click it
WebElement randomRow = driver.findElement(By.xpath("/html/body/div/section/div[3]/div/table/tbody/tr["+rowNum+"]/td"));
randomRow.click();
//Get Settings button and Click it
int rowNum2 = (rowNum + 1);
WebElement medicationName1 = driver.findElement(By.xpath("/html/body/div/section/div[3]/div/table/tbody/tr["+rowNum2+"]/td/div/article/section/ul/li/h1"));
WebElement dosage1 = driver.findElement(By.xpath("/html/body/div/section/div[3]/div/table/tbody/tr["+rowNum2+"]/td/div/article/section/ul/li[2]/h3"));
WebElement howMuch1= driver.findElement(By.xpath("/html/body/div/section/div[3]/div/table/tbody/tr["+rowNum2+"]/td/div/article/section/ul/li[3]/h3"));
WebElement reason1 = driver.findElement(By.xpath("/html/body/div/section/div[3]/div/table/tbody/tr["+rowNum2+"]/td/div/article/section/ul/li[4]/p"));
WebElement settingsBtn = driver.findElement(By.xpath("/html/body/div/section/div[3]/div/table/tbody/tr["+rowNum2+"]/td/div/article/aside/div/button"));
String medicationNameBefore = medicationName1.getText();
String dosageBefore = dosage1.getText();
String howMuchBefore = howMuch1.getText();
String reasonBefore = reason1.getText();
settingsBtn.click();
//Click Edit Button
WebElement editBtn = driver.findElement(By.xpath("/html/body/div/section/div[3]/div/table/tbody/tr["+rowNum2+"]/td/div/article/aside/div/ul/li/a"));
editBtn.click();
//Make Changes and click Cancel
WebElement medicationName2 = driver.findElement(By.xpath("/html/body/div/section/div[3]/div/table/tbody/tr["+rowNum2+"]/td/div/article/section/form/fieldset/h1"));
Select dosage2 = new Select (driver.findElement(By.xpath("/html/body/div/section/div[3]/div/table/tbody/tr["+rowNum2+"]/td/div/article/section/form/fieldset/label/select")));
WebElement howMuch2 = driver.findElement(By.xpath("/html/body/div/section/div[3]/div/table/tbody/tr["+rowNum2+"]/td/div/article/section/form/fieldset/label[2]/input"));
WebElement reason2 = driver.findElement(By.xpath("/html/body/div/section/div[3]/div/table/tbody/tr["+rowNum2+"]/td/div/article/section/form/fieldset/label[4]/input"));
medicationName2.sendKeys("TEST TEST TEST");
dosage2.selectByIndex(2);
howMuch2.sendKeys("TEST TEST TEST");
reason2.sendKeys("TEST TEST TEST");
WebElement cancelBtn = driver.findElement(By.xpath("/html/body/div/section/div[3]/div/table/tbody/tr["+rowNum2+"]/td/div/article/section/form/fieldset/div/button[2]"));
cancelBtn.click();
WebElement medicationName3 = driver.findElement(By.xpath("/html/body/div/section/div[3]/div/table/tbody/tr["+rowNum2+"]/td/div/article/section/ul/li/h1"));
WebElement dosage3 = driver.findElement(By.xpath("/html/body/div/section/div[3]/div/table/tbody/tr["+rowNum2+"]/td/div/article/section/ul/li[2]/h3"));
WebElement howMuch3= driver.findElement(By.xpath("/html/body/div/section/div[3]/div/table/tbody/tr["+rowNum2+"]/td/div/article/section/ul/li[3]/h3"));
WebElement reason3 = driver.findElement(By.xpath("/html/body/div/section/div[3]/div/table/tbody/tr["+rowNum2+"]/td/div/article/section/ul/li[4]/p"));
String medicationNameAfter = medicationName3.getText();
String dosageAfter = dosage3.getText();
String howMuchAfter = howMuch3.getText();
String reasonAfter = reason3.getText();
Assert.assertEquals(medicationNameBefore, medicationNameAfter);
Assert.assertEquals(dosageBefore, dosageAfter);
Assert.assertEquals(howMuchBefore, howMuchAfter);
Assert.assertEquals(reasonBefore, reasonAfter);
}
@Test(groups = {"functionalTests.FF"})
public void test_cancelButton_FF()
{
cancelButton("firefox");
}
@Test(groups = {"functionalTests.iOS"})
public void test_cancelButton_iOS()
{
cancelButton("iOS");
}
@AfterMethod(alwaysRun = true)
public void tearDown()
{
driver.quit();
}
}
テストを起動する私のXMLファイルは次のようになります。
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" parallel="false">
<test name="Test">
<groups>
<run>
<include name="functionalTests.FF"/>
<exclude name="functionalTests.iOS"/>
</run>
</groups>
<classes>
<class name="ui_Tests.AddPrescription" />
<class name="ui_Tests.ClickAddMedication"/>
<class name="ui_Tests.EditMedications_ValidateItems"/>
<class name="ui_Tests.EditMedications_MedicationNameReadOnly"/>
<class name="ui_Tests.EditMedications_EditStrengthForm"/>
<class name="ui_Tests.EditMedications_HowMuch"/>
<class name="ui_Tests.EditMedications_HowOften"/>
<class name="ui_Tests.EditMedications_Reason"/>
<class name="ui_Tests.EditMedications_Cancel"/>
<class name="ui_Tests.ExpandCollapseView"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
したがって、包含/除外を使用すると、テストを実行するブラウザーを指定できます。