Wednesday, 13 December 2017

Writing Steps and Pages (java code)

Now write steps according to the feature files

Example

package test.java.steps;

import org.testng.Assert;

import cucumber.api.PendingException;
import cucumber.api.java.en.Then;
import test.java.pages.ContentAdminPage;
import test.java.pages.HomePage;
import test.java.pages.PartnerHomePage;
import test.java.util.DriverFactory;
import test.java.util.VideoReord;

public class ContentAdminSteps extends DriverFactory {
 VideoReord videoStart = new VideoReord();

 @Then("^Web Content page should get opened$")
 public void web_Content_page_should_get_opened() throws Throwable {
  Assert.assertTrue(new ContentAdminPage(driver).isContentPageLoaded());
 }

 @Then("^Search and select a training class$")
 public void click_on_Browse_By_and_Click_on_Training_Classes() throws Throwable {
  new ContentAdminPage(driver).select_training_class();
 }

 @Then("^Web Content editor should opened$")
 public void web_Content_editor_should_opened() throws Throwable {
  Assert.assertTrue(new ContentAdminPage(driver).isWebContentEditorLoaded());
 }

 @Then("^Select Prerequesties Required as YES$")
 public void select_Prerequesties_Required_as_YES() throws Throwable {
  new ContentAdminPage(driver).Prerequesties_select();
 }

 @Then("^Click in publish$")
 public void click_in_publish() throws Throwable {
  new ContentAdminPage(driver).publish();
 }

 @Then("^Success Message should display$")
 public void success_Message_should_display() throws Throwable {
  Assert.assertTrue(new ContentAdminPage(driver).isSuccessMessageDisplayed());
 }

 @Then("^Search and select a training schedule$")
 public void search_and_select_a_training_schedule() throws Throwable {
  new ContentAdminPage(driver).select_training_schedule();
 }

 /*@Then("^Enter start date as \"([^\"]*)\" and end date as \"([^\"]*)\"$")
 public void enter_start_date_and_end_date(String startdate, String enddate) throws Throwable {
  new ContentAdminPage(driver).enter_date(startdate, enddate);
 }*/

 @Then("^Do logout$")
 public void do_logout() throws Throwable {
  new PartnerHomePage(driver).do_logout();
 }

 @Then("^Home Page should display$")
 public void home_Page_should_display() throws Throwable {
  new HomePage(driver).isLoginlinkDisplayed();

 }
}
package test.java.pages;

import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.Select;
import main.java.listener.DateFormatter;
import main.java.listener.Reporter;
import test.java.util.Screenshot;
import test.java.util.SeleniumControls;
import test.java.util.WebElementExtender;

public class ContentAdminPage {
 
 @FindBy(xpath = ".//*[@id='cpPortletTitle']/span[1]")
 private WebElement WebContentText;
 
 @FindBy(linkText = "Browse by Structure")
 private WebElement browseBy;
 
 @FindBy(linkText = "Training Classes")
 private WebElement trainigClasses;
 
 @FindBy(id = "_15_keywords")
 private WebElement searchBox;
 
 @FindBy(xpath = ".//*[@id='_15_simple']/div/button")
 private WebElement searchButton;
 
 
 @FindBy(linkText = "Hands-on Troubleshooting for 2000")
 private WebElement trainigclassLink;
 
 @FindBy(xpath = ".//*[@id='_15_fm2']/div[1]/a/div[2]/span[1]")
 private WebElement handson;
 
 @FindBy(xpath = ".//*[@id='_15_journalArticleBody']/div/div[2]/div/label")
 private WebElement title;
 
 @FindBy(name = "_15_prerequisites_required_INSTANCE_yayo")
 private WebElement preReqSelect;
 
 @FindBy(id = "_15_publishButton")
 private WebElement publishButton;
 

 @FindBy(xpath = ".//*[@id='portlet_15']/div/div/div/div[1]")
 private WebElement successMessage;
 
 @FindBy(id = "_15_start__date__INSTANCE__cgeo")
 private WebElement startDate;
 
 @FindBy(id = "_15_end__date__INSTANCE__dson")
 private WebElement endDate;
 
 @FindBy(xpath = ".//*[@id='_15_entriesContainer']/div[1]/a/div[2]/span[1]")
 private WebElement newSchedule;
 
 @FindBy(xpath = ".//*[@id='_15_publishButton']")
 private WebElement schedulePublish;
 
 WebDriver driver;
 Logger log = Logger.getLogger("");

 public ContentAdminPage(WebDriver driver) {
  this.driver = driver;
  PageFactory.initElements(driver, this);
 }

 public boolean isContentPageLoaded() throws IOException {
  
  try {
   log.info("Content Admin Page is Loaded Successfully");

  } catch (NoSuchElementException e) {
   log.error("Content Admin Page is not loaded");

   FileUtils.copyFile(Screenshot.captureElementBitmap(WebContentText),
     new File("D:/eclipse/WorkSpace/Yaskawa/Screenshots/WebContentText.png"));
   Reporter.addScreenCaptureFromPath("D:/eclipse/WorkSpace/Yaskawa/Screenshots/WebContentText.png");
  }
  
  return WebContentText.isDisplayed();
 }

 public void select_training_class() throws IOException, InterruptedException {
  
  try {
   log.info("search by keyword");
   WebElementExtender.highlightElement(searchBox);
   SeleniumControls.populateTextBox(searchBox, "Hands");
   //searchBox.sendKeys("Hands");
   
   log.info("click on search button");
   WebElementExtender.highlightElement(searchButton);
   searchButton.click();
   
   log.info("click on training class");
   WebElementExtender.highlightElement(handson);
   handson.click();
  } catch (NoSuchElementException e) {
   log.error("Trainig Class is not found");

   FileUtils.copyFile(Screenshot.captureElementBitmap(trainigClasses),
     new File("D:/eclectlipse/WorkSpace/Yaskawa/Screenshots/browseBy.png"));
   Reporter.addScreenCaptureFromPath("D:/eclipse/WorkSpace/Yaskawa/Screenshots/browseBy.png");
  }
 }

 public boolean isWebContentEditorLoaded() throws IOException {
  
  try {
   log.info("Web Content Editor is Loaded Successfully");

  } catch (NoSuchElementException e) {
   log.error("Web Content Editor is not loaded");

   FileUtils.copyFile(Screenshot.captureElementBitmap(WebContentText),
     new File("D:/eclipse/WorkSpace/Yaskawa/Screenshots/WebContentText.png"));
   Reporter.addScreenCaptureFromPath("D:/eclipse/WorkSpace/Yaskawa/Screenshots/WebContentText.png");
  }
  
  
  // TODO Auto-generated method stub
  return title.isDisplayed();
 }

 public void Prerequesties_select() throws IOException, InterruptedException {
  try {
   log.info("Prerequesties selection as yes");
   WebElementExtender.highlightElement(preReqSelect);
   Thread.sleep(6000);
   /*Select drpPreReq =new Select(preReqSelect);
   drpPreReq.selectByVisibleText("Yes");
   */
   SeleniumControls.populateDropDown(preReqSelect, "Yes");

  } catch (NoSuchElementException e) {
   log.error("No option is selected");

   FileUtils.copyFile(Screenshot.captureElementBitmap(WebContentText),
     new File("D:/eclipse/WorkSpace/Yaskawa/Screenshots/WebContentText.png"));
   Reporter.addScreenCaptureFromPath("D:/eclipse/WorkSpace/Yaskawa/Screenshots/WebContentText.png");
  }
  
 }

 public void publish() throws IOException, InterruptedException {
  
  try {
   log.info("Click in Publish");
   WebElementExtender.highlightElement(publishButton);
   publishButton.click();
   Thread.sleep(6000);

  } catch (NoSuchElementException e) {
   log.error("Some mandatory fields are not filled");

   FileUtils.copyFile(Screenshot.captureElementBitmap(WebContentText),
     new File("D:/eclipse/WorkSpace/Yaskawa/Screenshots/WebContentText.png"));
   Reporter.addScreenCaptureFromPath("D:/eclipse/WorkSpace/Yaskawa/Screenshots/WebContentText.png");
  }
   
 }

 public boolean isSuccessMessageDisplayed() throws IOException {
  
  try {
   log.info("Success Message is displaying");
   WebElementExtender.highlightElement(successMessage);
   

  } catch (NoSuchElementException e) {
   log.error("Success Message is not displaying");

   FileUtils.copyFile(Screenshot.captureElementBitmap(WebContentText),
     new File("D:/eclipse/WorkSpace/Yaskawa/Screenshots/WebContentText.png"));
   Reporter.addScreenCaptureFromPath("D:/eclipse/WorkSpace/Yaskawa/Screenshots/WebContentText.png");
  }
  // TODO Auto-generated method stub
  return successMessage.isDisplayed();
 }


 public void select_training_schedule() throws IOException {
  try {
   log.info("search by trainig schedule");
   WebElementExtender.highlightElement(searchBox);
   SeleniumControls.populateTextBox(searchBox, "New Schedule");
   //searchBox.sendKeys("New Schedule");
   
   log.info("click on search button");
   WebElementExtender.highlightElement(searchButton);
   searchButton.click();
   
   log.info("click on training schedule");
   WebElementExtender.highlightElement(newSchedule);
   newSchedule.click();
  } catch (NoSuchElementException e) {
   log.error("Trainig Schedule  is not found");

   FileUtils.copyFile(Screenshot.captureElementBitmap(newSchedule),
     new File("D:/eclectlipse/WorkSpace/Yaskawa/Screenshots/browseBy.png"));
   Reporter.addScreenCaptureFromPath("D:/eclipse/WorkSpace/Yaskawa/Screenshots/browseBy.png");
  }
  
 }
 
public void enter_date(String start, String end) throws IOException {
  
  try {
   log.info("Enter Start Date");
   WebElementExtender.highlightElement(startDate);
   SeleniumControls.populateTextBox(startDate,DateFormatter.getStartDate(start));
   
   log.info("Enter end Date");
   WebElementExtender.highlightElement(endDate);
   SeleniumControls.populateTextBox(endDate, DateFormatter.geEndtDate(end));
   

  } catch (NoSuchElementException e) {
   log.error("Success Message is not displaying");

   FileUtils.copyFile(Screenshot.captureElementBitmap(WebContentText),
     new File("D:/eclipse/WorkSpace/Yaskawa/Screenshots/WebContentText.png"));
   Reporter.addScreenCaptureFromPath("D:/eclipse/WorkSpace/Yaskawa/Screenshots/WebContentText.png");
  }
 }

 public void do_publish() {
  // TODO Auto-generated method stub
  
 }

}

No comments:

Post a Comment

Set up testlink with Selenium Framework

To Integrate Testlink with selenium add following jars in your setup testlink-api-client-2.0.jar xmlrpc-common-3.1.jar xmlrpc-client-...