1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import org.testng.Assert; import org.testng.annotations.Test; public class Ajaxdemo { static WebDriver driver; //Creating reference variable of Webdriver static WebDriverWait wait; //Creating WebDriverWait reference variable @Test public void ajax_logic_try() //Try to click on WebElement which is dynamic on webpage { // Setting up the properties for Chrome Driver System.setProperty("webdriver.chrome.driver", "C:\\Vision\\chromedriver.exe"); // Inserting Chromedriver to Webdriver refernece object driver = new ChromeDriver(); // Maximizing the browser window driver.manage().window().maximize(); // Passing the URL driver.get("https://www.moneycontrol.com/"); // Providing wait to load all the elements on page driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); // logic try { // Inserting WebdriverWait with "driver" reference and timeout for 5 Seconds wait = new WebDriverWait(driver, 5); // Saving WebElement having hyperlinking in crudeOil_text vaeriable WebElement crudeOil_text = driver.findElement(By.linkText("CRUDEOIL")); // wait until WebElement having hyperlinking in crudeOil_text is visible on webpage wait.until(ExpectedConditions.visibilityOf(crudeOil_text)); // clicking on WebElement crudeOil_text.click(); // Getting value of WebElement inside Get_Text_Ajax String Get_Text_Ajax = crudeOil_text.getText(); // Inserting "CRUDEOIL" as text inside Exp_Text_Ajax String Exp_Text_Ajax = "CRUDEOIL"; // Inserting the text inside WebElement inside Act_text_Ajax String Act_text_Ajax = Get_Text_Ajax; // Performing Hard Assertion with text on Actual WebElement and Expected Text to be on WebElement Assert.assertEquals(Act_text_Ajax, Exp_Text_Ajax, "The actual WebElement 'CRUDEOIL' is not Present On page"); // Printing the Actual Text from the WebElement from the webpage System.out.println("The Text On Link is " + Act_text_Ajax); // Quitting the driver driver.quit(); } catch (Exception NoSuchElementException) // Catching if there is 'NoSuchElementException' situation { // Error message if Exception Arise System.out.println("Try to execute code once Again NoSuchElementException is ocurred"); } } } |