Hướng dẫn css and xpath checker - trình kiểm tra css và xpath
nhập khẩu org.openqa.selenium.by;org.openqa.selenium.By; nhập khẩu org.openqa.selenium.webdriver;org.openqa.selenium.WebDriver; nhập khẩu org.openqa.selenium.webelement;org.openqa.selenium.WebElement; nhập khẩu org.openqa.selenium.chrom.chromedriver;org.openqa.selenium.chrome.ChromeDriver; nhập org.openqa.selenium.support.ui.expectedConditions;org.openqa.selenium.support.ui.ExpectedConditions; nhập khẩu org.openqa.selenium.support.ui.webdriverwait;org.openqa.selenium.support.ui.WebDriverWait; publicclassSeleniumEx{classSeleniumEx{ & nbsp;staticWebDriverWait wait; & nbsp;static WebDriver webDriver; publicstaticvoidmain(String[]args)throwsInterruptedException{publicstaticvoidmain(String[]args)throwsInterruptedException{ & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;//Define the path of ChromeDriver StringchromDriverPath="src/main/resources/SeleniumWebDriver/chromedriver.exe";StringchromDriverPath="src/main/resources/SeleniumWebDriver/chromedriver.exe"; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;//Set the property of WebDriver System.setProperty("webdriver.chrome.driver",chromDriverPath);System.setProperty("webdriver.chrome.driver", chromDriverPath); & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; ////Create a Chrome Driver webDriver=newChromeDriver();webDriver=newChromeDriver(); & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;//Define the wait object with 5 sec time out wait=newWebDriverWait(webDriver,5);wait=newWebDriverWait(webDriver,5); & nbsp; & nbsp; & nbsp; & nbsp;//Set delay time to see how Selenium work on Web browser intdelay=100;int delay=100; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;//The URL of Ginger spelling checker StringgingerUrl="https://www.gingersoftware.com/spellcheck";StringgingerUrl="https://www.gingersoftware.com/spellcheck"; & nbsp; & nbsp;//Use Chrome to access the url webDriver.get(gingerUrl);webDriver.get(gingerUrl); & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;//An example of a paragraph containing misspelling words & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;StringmispellText="Many handicraft village in Hanoi still struggling to building a brand for "+ & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;"themselves in the market economic."+ & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;"Van Phuc Village has finding a way to preserved the craft traditional of silk." + & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;"the village are well-know to the finestest silk in Vietnam."+ & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;"villagers are prouded of their products, which have establihed a name for "+ & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;"themselves in domesti and wod markets."; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;//Split into sentence to examine Ginger many times & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;String[]mispellSentence=mispellText.split("\\."); & NBSP; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;// Get the cover of the text area & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;WebElement originalTextOriginal=getElement("//*[@id=\"GingerWidget-originalHtmlText\"]"); originalTextOriginal.click();originalTextOriginal.click(); & NBSP; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;//Get the submit text area & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;WebElement textArea=getElement("//*[@id=\"GingerWidget-originalTexInput\"]"); & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;//Get the 'Go' button & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;WebElement submit=getElement("//*[@id=\"GingerWidget-submitButton\"]"); & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;//Get the correct text element & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;WebElement correctTextElement; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;//Check the spelling of each sentence for(StringoriginalText:mispellSentence){for(StringoriginalText:mispellSentence){ & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;//Clean the previous text textArea.clear();textArea.clear(); Thread.sleep(delay);Thread.sleep(delay); & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;//Submit the text textArea.sendKeys(originalText);textArea.sendKeys(originalText); Thread.sleep(delay);Thread.sleep(delay); & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;//Click 'Go' button submit.click();submit.click(); Thread.sleep(delay);Thread.sleep(delay); & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;//Get the corrected text & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;correctTextElement=getElement("//*[@id=\"GingerWidget-correctedText\"]"); Thread.sleep(5000);Thread.sleep(5000); & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;//Display the result StringcorrectText=correctTextElement.getText();StringcorrectText =correctTextElement.getText(); & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;System.out.println("The original text: "+originalText); & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;System.out.println("Using Ginger App: "+correctText); System.out.println();System.out.println(); & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;//Click to check another sentence originalTextOriginal.click();originalTextOriginal.click(); }} }} /**/** & nbsp; & nbsp; & nbsp; & nbsp; * Nhận một phần tử cụ thể bằng cách sử dụng XPath của nó & nbsp; & nbsp; & nbsp; & nbsp; * @param xpath the xpath của phần tử & nbsp; & nbsp; & nbsp; & nbsp; * @return phần tử web & nbsp; & nbsp; & nbsp; & nbsp; */ & nbsp; & nbsp;private staticWebElement getElement(StringxPath){ & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbs//Wait until the element is visible wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(xPath)));wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(xPath))); returnwebDriver.findElement(By.xpath(xPath));returnwebDriver.findElement(By.xpath(xPath)); }} } |