Created
September 8, 2014 12:55
-
-
Save JoaoGFarias/73380ef76bd153540ea7 to your computer and use it in GitHub Desktop.
Selenium - Parallel Data-Driven testing using TestNG
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*This program is free software: you can redistribute it and/or modify | |
it under the terms of the GNU General Public License as published by | |
the Free Software Foundation, either version 3 of the License, or | |
(at your option) any later version. | |
This program is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
GNU General Public License for more details. | |
You should have received a copy of the GNU General Public License | |
along with this program. If not, see <http://www.gnu.org/licenses/>. | |
Tests the BMI calculator on http://www.nhlbi.nih.gov/health/educational/lose_wt/BMI/bmi-m.htm | |
The last test shall fail | |
*/ | |
public class dataDriven { | |
//WebDriver driver; | |
@DataProvider(name = "dp", parallel = true) | |
public Object[][] dataProvider() { | |
return new Object[][] { | |
new Object[] { "160", "50","19.5" }, | |
new Object[] { "190", "90","24.9" }, | |
new Object[] { "210", "170","38.5" }, | |
new Object[] { "130", "60","30.5" }, // Shall fail | |
}; | |
} | |
@BeforeTest | |
public void beforeTest() { | |
} | |
@AfterTest | |
public void afterTest() { | |
//driver.quit(); | |
} | |
@Test(dataProvider = "dp") | |
public void testBMICalculator(String height, String weight, String bmi){ | |
/* To run concurrently, it`s necessary to create individual drivers for each | |
data row*/ | |
WebDriver driver = new SafariDriver(); | |
driver.get("http://www.nhlbi.nih.gov/health/educational/lose_wt/BMI/bmi-m.htm"); | |
WebElement heightInput = driver.findElement(By.id("htc")); | |
heightInput.clear(); | |
heightInput.sendKeys(height); | |
WebElement weightInput = driver.findElement(By.id("kg")); | |
weightInput.clear(); | |
weightInput.sendKeys(weight); | |
List<WebElement> buttons = driver.findElements(By.tagName("input")); | |
for(WebElement button : buttons){ | |
String buttonValue = button.getAttribute("value"); | |
if("compute bmi".equals(buttonValue.toLowerCase())){ | |
button.click(); | |
break; | |
} | |
} | |
if(!"0.0".equals(weight)){ | |
WebDriverWait wait = new WebDriverWait(driver,10); | |
wait.until(new ExpectedCondition<Boolean>() { | |
public Boolean apply(WebDriver driver) { | |
WebElement button = driver.findElement(By.name("bmi")); | |
String bmiValue = button.getAttribute("value"); | |
return !bmiValue.equals("0.0"); | |
} | |
}); | |
} | |
WebElement bmiResult = driver.findElement(By.name("bmi")); | |
Assert.assertEquals(bmiResult.getAttribute("value"),bmi); | |
driver.quit(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment