我正在Google Translate页面上测试Selenium-Webdriver。我想做的是:
案例1:输入和输出语言已经呈现并且结果符合预期,我没有任何问题。但是对于案例2:输出语言尚未呈现并且必须在表格中选择,则会出现问题。 所选语言不正确(例如,我选择挪威语,但最后会选择萨摩亚语)。 选择输入使用相同的方式进行选择,但根本不会返回任何问题。
package TestNG;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.*;
public class GoogleTranslate {
WebDriver driver;
@BeforeMethod
public void setUp() {
System.setProperty("webdriver.gecko.driver","F:\\path\\geckodriver.exe");
driver=new FirefoxDriver();
}
@Test(priority=0)
public void VietnameseToEnglish() throws InterruptedException {
// Go to google translate
driver.manage().window().maximize();
driver.get("https://translate.google.com/");
Thread.sleep(5000);
// Check if the page is correct
String currentTitle = driver.getTitle();
Assert.assertEquals(currentTitle, "Google Translate");
Thread.sleep(3000);
// Select Vietnamese as input
if (driver.findElements(By.xpath("//div[@id='gt-lang-left']/div[@id='gt-lang-src']/div[@id='gt-sl-sugg']/div[contains(@class,'goog-inline-block jfk-button jfk-button-standard') and @value='vi']")).size() > 0)
{
WebElement Vi = driver.findElement(By.xpath("//div[@id='gt-lang-left']/div[@id='gt-lang-src']/div[@id='gt-sl-sugg']/div[contains(@class,'goog-inline-block jfk-button jfk-button-standard') and @value='vi']"));
String isViSelected = Vi.getAttribute("aria-pressed");
if (isViSelected.equals("false"))
{
Vi.click();
}
}
else
{
WebElement allLangSrc = driver.findElement(By.id("gt-sl-gms"));
allLangSrc.click();
WebElement ViSrc = driver.findElement(By.xpath("//div[@id='goog-menuitem-group-7']/div[@id=':2r']/div"));
ViSrc.click();
}
// Select English as output
if (driver.findElements(By.xpath("//div[@id='gt-lang-right']/div[@id='gt-lang-tgt']/div[@id='gt-tl-sugg']/div[contains(@class,'goog-inline-block jfk-button jfk-button-standard') and @value='en']")).size() > 0)
{
WebElement En = driver.findElement(By.xpath("//div[@id='gt-lang-right']/div[@id='gt-lang-tgt']/div[@id='gt-tl-sugg']/div[contains(@class,'goog-inline-block jfk-button jfk-button-standard') and @value='en']"));
String isEnSelected = En.getAttribute("aria-pressed");
if (isEnSelected.equals("false"))
{
En.click();
}
}
else
{
WebElement allLangTrg = driver.findElement(By.id("gt-tl-gms"));
allLangTrg.click();
WebElement EnTrg = driver.findElement(By.xpath("//div[@id='goog-menuitem-group-2']/div[@id=':3j']/div"));
EnTrg.click();
}
WebElement Input = driver.findElement(By.id("source-is"));
//Input.clear();
Input.sendKeys("Ga");
WebElement TranslateButton = driver.findElement(By.id("gt-submit"));
TranslateButton.click();
Thread.sleep(3000);
WebElement Output = driver.findElement(By.xpath("//span[@id='result_box']/span"));
String Outtext = Output.getText();
Assert.assertEquals(Outtext, "Chicken");
}
@Test(priority=1)
public void VietnameseToNorg() throws InterruptedException {
// Go to google translate
driver.manage().window().maximize();
driver.get("https://translate.google.com/");
Thread.sleep(5000);
// Check if the page is correct
String currentTitle = driver.getTitle();
Assert.assertEquals(currentTitle, "Google Translate");
Thread.sleep(3000);
// Select Vietnamese as input
if (driver.findElements(By.xpath("//div[@id='gt-lang-left']/div[@id='gt-lang-src']/div[@id='gt-sl-sugg']/div[contains(@class,'goog-inline-block jfk-button jfk-button-standard') and @value='vi']")).size() > 0)
{
WebElement Vi = driver.findElement(By.xpath("//div[@id='gt-lang-left']/div[@id='gt-lang-src']/div[@id='gt-sl-sugg']/div[contains(@class,'goog-inline-block jfk-button jfk-button-standard') and @value='vi']"));
String isViSelected = Vi.getAttribute("aria-pressed");
if (isViSelected.equals("false"))
{
Vi.click();
}
}
else
{
WebElement allLangSrc = driver.findElement(By.id("gt-sl-gms"));
allLangSrc.click();
WebElement ViSrc = driver.findElement(By.xpath("//div[@id='goog-menuitem-group-7']/div[@id=':2r']/div"));
ViSrc.click();
}
// Select Norg as output
if (driver.findElements(By.xpath("//div[@id='gt-lang-right']/div[@id='gt-lang-tgt']/div[@id='gt-tl-sugg']/div[contains(@class,'goog-inline-block jfk-button jfk-button-standard') and @value='no']")).size() > 0)
{
WebElement No = driver.findElement(By.xpath("//div[@id='gt-lang-right']/div[@id='gt-lang-tgt']/div[@id='gt-tl-sugg']/div[contains(@class,'goog-inline-block jfk-button jfk-button-standard') and @value='no']"));
String isNoSelected = No.getAttribute("aria-pressed");
if (isNoSelected.equals("false"))
{
No.click();
}
}
else
{
WebElement allLangTrg = driver.findElement(By.id("gt-tl-gms"));
allLangTrg.click();
WebElement NoTrg = driver.findElement(By.xpath("//div[@id='goog-menuitem-group-5']/div[@id=':52']/div"));
NoTrg.click();
}
WebElement Input = driver.findElement(By.id("source-is"));
//Input.clear();
Input.sendKeys("Sua");
WebElement TranslateButton = driver.findElement(By.id("gt-submit"));
TranslateButton.click();
Thread.sleep(3000);
WebElement Output = driver.findElement(By.xpath("//span[@id='result_box']/span"));
String Outtext = Output.getText();
Assert.assertEquals(Outtext, "Melk");
}
@AfterMethod
public void tearDown(){
driver.close();
driver.quit();
}
}
答案 0 :(得分:1)
我会做这样的事情。您应该专注于创建执行单一操作的函数,以便您拥有可以在各种测试中轻松使用的可重用代码。
以下是我的功能
static public String getTranslatedText()
{
new WebDriverWait(driver, 10).until(new ExpectedCondition<Boolean>()
{
public Boolean apply(WebDriver d)
{
return d.findElement(By.id("result_box")).getText().length() != 0;
}
});
return driver.findElement(By.id("result_box")).getText();
}
static public void setSourceLanguage(String lang)
{
// see if the desired language is already selected
if (!driver.findElement(By.cssSelector("#gt-lang-src div.jfk-button-checked")).getText().equals(lang))
{
// open the Source language dropdown
driver.findElement(By.id("gt-sl-gms")).click();
// select the desired language
driver.findElement(By.xpath("//div[@id='gt-sl-gms-menu']//div[text()='" + lang + "']")).click();
}
}
static public void setTargetLanguage(String lang)
{
// see if the desired language is already selected
if (!driver.findElement(By.cssSelector("#gt-lang-tgt div.jfk-button-checked")).getText().equals(lang))
{
// open the Source language dropdown
driver.findElement(By.id("gt-tl-gms")).click();
// select the desired language
driver.findElement(By.xpath("//div[@id='gt-tl-gms-menu']//div[text()='" + lang + "']")).click();
}
}
static public void translateText(String text)
{
driver.findElement(By.id("source")).sendKeys(text);
}
简单的测试看起来像
driver.get("https://translate.google.com/");
setSourceLanguage("English");
setTargetLanguage("Lao");
translateText("This is some text");
System.out.println(getTranslatedText());
完成本练习后,我注意到文本翻译后的网址为https://translate.google.com/#en/lo/This%20is%20some%20text。我假设您不是在测试谷歌翻译,而只是想要翻译?如果这是真的,您可以根据语言标记导航到URL并传入所需的文本。
网址格式为
https://translate.google.com/#<source language token>/<target language token>/<text to be translated>
所以越南语到英语就是
https://translate.google.com/#vi/en/This%20is%20some%20text
和越南语到挪威语
https://translate.google.com/#vi/no/This%20is%20some%20text
然后你所要做的就是抓住翻译的文本。