Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.83 KB | None | 0 0
  1. Code:
  2. package Test_package;
  3.  
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileNotFoundException;
  7. import java.io.IOException;
  8. import java.net.MalformedURLException;
  9. import java.net.URL;
  10.  
  11. import org.openqa.selenium.By;
  12. import org.openqa.selenium.JavascriptExecutor;
  13. import org.openqa.selenium.Platform;
  14. import org.openqa.selenium.WebDriver;
  15. import org.openqa.selenium.WebElement;
  16. import org.openqa.selenium.chrome.ChromeDriver;
  17. import org.openqa.selenium.remote.DesiredCapabilities;
  18. import org.openqa.selenium.remote.RemoteWebDriver;
  19. import org.openqa.selenium.support.ui.Select;
  20. import org.testng.ITestResult;
  21. import org.testng.annotations.AfterMethod;
  22. import org.testng.annotations.AfterTest;
  23. import org.testng.annotations.BeforeMethod;
  24. import org.testng.annotations.BeforeTest;
  25. import org.testng.annotations.DataProvider;
  26. import org.testng.annotations.Test;
  27.  
  28. import com.relevantcodes.extentreports.ExtentReports;
  29. import com.relevantcodes.extentreports.ExtentTest;
  30. import com.relevantcodes.extentreports.LogStatus;
  31.  
  32. import jxl.Sheet;
  33. import jxl.Workbook;
  34. import jxl.read.biff.BiffException;
  35.  
  36. public class datadriven_coop {
  37. WebDriver driver;
  38. ExtentReports extent;
  39. ExtentTest logger;
  40. String baseUrl, nodeUrl;
  41.  
  42. @BeforeTest
  43. public void startReport(){
  44.  
  45. extent = new ExtentReports (System.getProperty("user.dir") +"/test-output/STMExtentReport.html", true);
  46.  
  47. extent
  48. .addSystemInfo("Host Name", "SoftwareTestingMaterial")
  49. .addSystemInfo("Environment", "Automation Testing")
  50. .addSystemInfo("User Name", "Pratiksha Salunkhe");
  51.  
  52.  
  53. extent.loadConfig(new File(System.getProperty("user.dir")+"/extent-config.xml"));
  54. }
  55. @BeforeMethod
  56. public void beforeMethod() throws MalformedURLException, InterruptedException {
  57.  
  58.  
  59.  
  60. System.setProperty("webdriver.chrome.driver","C:\Chrome\chromedriver.exe");
  61. driver = new ChromeDriver();
  62. }
  63.  
  64. @DataProvider(name="Cooplogin")
  65. public Object[][] loginData() throws BiffException, IOException {
  66. Object[][] arrayObject = getExcelData("C:\Standalone\ABC.xls","Sheet2");
  67. return arrayObject;
  68. }
  69.  
  70.  
  71. public String[][] getExcelData(String fileName, String sheetName) throws BiffException, IOException {
  72. String[][] arrayExcelData = null;
  73. try
  74. {
  75. FileInputStream fs = new FileInputStream(fileName);
  76. Workbook wb = Workbook.getWorkbook(fs);
  77. Sheet sh = wb.getSheet(sheetName);
  78.  
  79. int totalNoOfCols = sh.getColumns();
  80. int totalNoOfRows = sh.getRows();
  81. System.out.println(totalNoOfCols);
  82. System.out.println(totalNoOfRows);
  83.  
  84. arrayExcelData = new String[totalNoOfRows-1][totalNoOfCols];
  85.  
  86. for (int i= 1 ; i<totalNoOfRows; i++) {
  87.  
  88. for (int j=0; j<totalNoOfCols; j++) {
  89. arrayExcelData[i-1][j] = sh.getCell(j, i).getContents();
  90. }
  91.  
  92. }
  93.  
  94.  
  95. }
  96.  
  97.  
  98. catch (FileNotFoundException e) {
  99. e.printStackTrace();
  100. } catch (IOException e) {
  101. e.printStackTrace();
  102. e.printStackTrace();
  103. } catch (BiffException e) {
  104. e.printStackTrace();
  105. }
  106. return arrayExcelData;
  107.  
  108. }
  109.  
  110. @Test(dataProvider="Cooplogin")
  111. public void Coop_produkt(String Username,String password,String Category,String Subcategory,String Category1,String Subcategory1) throws InterruptedException {
  112.  
  113. logger = extent.startTest("Coop_produkt");
  114. driver.get("https://www.coopathome.ch/de/");
  115.  
  116. Thread.sleep(5000);
  117.  
  118.  
  119. //fruits
  120. driver.findElement(By.xpath(".//*[@id='nav']/ul/li[1]/a/span")).click();
  121. Thread.sleep(2000);
  122. driver.findElement(By.xpath(".//*[@id='subnav--item-m_0001']/li[1]/ul[1]/li[1]/a")).click();
  123. Thread.sleep(2000);
  124. JavascriptExecutor jse8= (JavascriptExecutor)driver;
  125. jse8.executeScript("window.scrollBy(0,400)", "");
  126. driver.findElement(By.xpath("html/body/div[3]/div/div[2]/div[2]/div/div[4]/ul/li[1]/div/div/div[3]/button")).click();
  127. Thread.sleep(2000);
  128. driver.findElement(By.id("j_username")).sendKeys(Username);
  129. driver.findElement(By.id("j_password")).sendKeys(password);
  130. driver.findElement(By.id("loginOverlaySubmitLoginFormButton")).click();
  131. Thread.sleep(2000);
  132.  
  133. driver.findElement(By.xpath("html/body/div[3]/div/div[2]/div[2]/div/div[4]/ul/li[1]/div/div/div[3]/div/div/button[2]")).click();
  134.  
  135. //Shampoo & Salad
  136. Thread.sleep(4000);
  137. driver.findElement(By.xpath(".//*[contains(text(),'" +Category+ "')]")).click();
  138.  
  139. Thread.sleep(4000);
  140. driver.findElement(By.xpath(".//*[contains(text(),'" +Subcategory+ "')]")).click();
  141. Thread.sleep(4000);
  142. driver.findElement(By.xpath(".//*[@id='sortOptions']")).click();
  143. Select dropdown1=new Select(driver.findElement(By.name("sort")));
  144. dropdown1.selectByVisibleText("Preis (absteigend)");
  145. JavascriptExecutor jse7= (JavascriptExecutor)driver;
  146. jse7.executeScript("window.scrollBy(0,400)", "");
  147. driver.findElement(By.xpath("html/body/div[3]/div/div[2]/div[2]/div/div[4]/ul/li[1]/div/div/div[3]/button")).click();
  148. Thread.sleep(4000);
  149. driver.findElement(By.xpath("html/body/div[3]/div/div[2]/div[2]/div/div[4]/ul/li[1]/div/div/div[3]/div/div/button[2]")).click();
  150.  
  151. //Drinks & Beer
  152. Thread.sleep(4000);
  153. driver.findElement(By.xpath(".//*[contains(text(),'" +Category1+ "')]")).click();
  154. Thread.sleep(4000);
  155. driver.findElement(By.xpath(".//*[contains(text(),'" +Subcategory1+ "')]")).click();
  156. Thread.sleep(4000);
  157. driver.findElement(By.xpath(".//*[@id='sortOptions']")).click();
  158. Select dropdown2=new Select(driver.findElement(By.name("sort")));
  159. dropdown2.selectByVisibleText("Preis (absteigend)");
  160. JavascriptExecutor jse6= (JavascriptExecutor)driver;
  161. jse6.executeScript("window.scrollBy(0,500)", "");
  162. driver.findElement(By.xpath("html/body/div[3]/div/div[2]/div[2]/div/div[4]/ul/li[1]/div/div/div[3]/button")).click();;
  163. Thread.sleep(4000);
  164. driver.findElement(By.xpath("html/body/div[3]/div/div[2]/div[2]/div/div[4]/ul/li[1]/div/div/div[3]/div/div/button[2]")).click();
  165. Thread.sleep(4000);
  166.  
  167. //add to cart
  168. driver.findElement(By.id("miniCartProceedToCheckoutButton")).click();
  169. Thread.sleep(4000);
  170. driver.findElement(By.id("miniCartProceedToCheckoutButton")).click();
  171. Thread.sleep(4000);
  172.  
  173. //Date for delivery
  174. driver.findElement(By.xpath(".//*[@id='heimlieferung']/div[4]/dl/dd[3]/a")).click();
  175. driver.findElement(By.id("chooseDeliveryDate_continue_button")).click();
  176. WebElement t=driver.findElement(By.xpath("html/body/div[3]/div/div[3]/div/dd"));
  177. if (t.getText().equals("120.40"))
  178. {
  179. System.out.println("Gesamtkosten ist korrekt");
  180. logger.log(LogStatus.INFO, "Validierung erfolgreich : Gesamtkosten ist korrekt");
  181.  
  182. }
  183. else {
  184. System.out.println("Gesamtkosten ist nicht korrekt, Erwarteten Gesamtkosten ist "+ t.getText());
  185. }
  186. JavascriptExecutor jse18= (JavascriptExecutor)driver;
  187. jse18.executeScript("window.scrollBy(0,500)", "");
  188. driver.findElement(By.xpath("html/body/div[3]/div/div[1]/div/div/div[5]/form/div[2]/p/a")).click();
  189.  
  190. driver.findElement(By.xpath("html/body/div[3]/div/div[2]/div[1]/div[2]/div[2]/span[2]/a[1]")).click();
  191.  
  192.  
  193. //Alert alert=driver.switchTo().alert();
  194. Thread.sleep(4000);
  195. if(driver.findElement(By.id("removeAllItemsBtn")).isDisplayed())
  196. {
  197. driver.findElement(By.id("removeAllItemsBtn")).click();
  198. }
  199. driver.findElement(By.xpath("html/body/div[2]/div/div[2]/nav[2]/div/ul/li[7]/a")).click();
  200. System.out.println("Jetzt ist den Warenkorb leer und Sie können neues Produkt wieder hinzufügen");
  201. logger.log(LogStatus.PASS, "Jetzt ist den Warenkorb leer und Sie koennen neues Produkt wieder hinzufuegen");
  202. logger.log(LogStatus.PASS, "Coop_produkt Testfall ist erfolgreicht");
  203. }
  204.  
  205.  
  206. @AfterMethod
  207. public void getResult(ITestResult result){
  208.  
  209. if(result.getStatus() == ITestResult.FAILURE){
  210. logger.log(LogStatus.FAIL, "Test Case Failed is "+result.getName());
  211. logger.log(LogStatus.FAIL, "Test Case Failed is "+result.getThrowable());
  212. }else if(result.getStatus() == ITestResult.SKIP){
  213. logger.log(LogStatus.SKIP, "Test Case Skipped is "+result.getName());
  214. }
  215.  
  216. extent.endTest(logger);
  217. }
  218.  
  219.  
  220. @AfterTest
  221. public void endReport(){
  222.  
  223. extent.flush();
  224.  
  225. }
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement