我想測驗注冊頁面,其中我撰寫了一個代碼,該代碼將提供網站所需的所有必需詳細資訊,但在填寫所有必填欄位后,它應該單擊Sign Up將重定向到下一個 URL的按鈕。一次,它被重定向到下一個 URL,然后驗證通過,否則失敗。為了測驗我存盤到變數中的 URL,u但是當我執行代碼時,u我只獲得了原始 URL,而不是重定向到發布 URL。
代碼:
package Seleniumtesting;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
public class Selenium {
ChromeDriver driver;
String url ="https://login.mailchimp.com/signup/";
public void invokeBrowser() {
try {
System.setProperty("webdriver.chrome.driver","C:\\Users\\hp\\Desktop\\Selenium\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
TimeUnit.SECONDS.sleep(2);
driver.manage().deleteAllCookies();
driver.get(url);
String urlFromWebpage = driver.getCurrentUrl();
if(urlFromWebpage.equals("https://login.mailchimp.com/signup/")) {
System.out.println("PASS");
}
else {
System.out.println("FAIL");
}
}
catch(InterruptedException ex)
{
ex.printStackTrace();
}
}
public void signup(){
try {
driver.manage().window().maximize();
WebElement createAccountHeading = driver.findElement(By.xpath("//span[text()='Create an account or ']"));
if(createAccountHeading.isDisplayed()) {
System.out.println("PASS");
}else
System.out.println("FAIL");
driver.findElement(By.name("email")).sendKeys("[email protected]");
driver.findElement(By.name("username")).sendKeys("Testvidwqrdee1243");
driver.findElement(By.name("password")).sendKeys("Test123");
TimeUnit.SECONDS.sleep(2);
driver.findElement(By.name("marketing_newsletter")).click();
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL, Keys.END);
TimeUnit.SECONDS.sleep(2);
driver.findElement(By.xpath("//button[@id='create-account']")).click();
TimeUnit.SECONDS.sleep(5);
driver.findElement(By.xpath("//h1[text()='Check your email']"));
// System.out.println(driver.getCurrentUrl());
String u = driver.getCurrentUrl();
System.out.println("URL: " u);
if(u.contains("success"))
{
System.out.println("PASS !!");
}
else
{
System.out.println("FAIL !!");
}
driver.close();
}
catch(InterruptedException ex)
{
ex.printStackTrace();
}
}
public static void main(String[] args) {
Selenium mc = new Selenium();
mc.invokeBrowser();
mc.signup();
}
}
uj5u.com熱心網友回復:
理想的情況下驗證之前CURRENTURL需要引起WebDriverWait用于visibilityOfElementLocated()對任何的可見的內元素DOM樹。例如,等待h1帶有文本的Welcome to Mailchimp如下所示:
driver.get(url);
String urlFromWebpage = driver.getCurrentUrl();
new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//h1[contains(., 'Welcome to Mailchimp')]")));
if(urlFromWebpage.equals("https://login.mailchimp.com/signup/")) {
System.out.println("PASS");
}
else {
System.out.println("FAIL");
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/409791.html
標籤:
下一篇:如何按下帶有父類名稱的按鈕?
