Java 在主方法之后不执行下一个方法
我必须删除其中第一个文件包含登录详细信息的文件。第一个文件是loginDetailsClass.java,下面是文件中的代码Java 在主方法之后不执行下一个方法,java,selenium,automated-tests,Java,Selenium,Automated Tests,我必须删除其中第一个文件包含登录详细信息的文件。第一个文件是loginDetailsClass.java,下面是文件中的代码 public class loginDetailsClass { @SuppressWarnings("deprecation") public static void browserLaunch() { WebDriver driver; String baseurl = "https://www.facebook.c
public class loginDetailsClass {
@SuppressWarnings("deprecation")
public static void browserLaunch() {
WebDriver driver;
String baseurl = "https://www.facebook.com/";
System.setProperty("webdriver.chrome.driver","D:\\eclipse-workspace\\chromedriver.exe");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.switches", Arrays.asList("--incognito"));
ChromeOptions options = new ChromeOptions();
options.addArguments("--test-type");
options.addArguments("--disable-extensions");
options.addArguments("--disable-infobars");
capabilities.setCapability("chrome.binary","D:\\eclipse-workspace\\chromedriver.exe");
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
driver = new ChromeDriver(capabilities);
driver.manage().deleteAllCookies();
driver.manage().window().maximize();
driver.get(baseurl);
//Login section
try {
Thread.sleep(7000); // 1000 milliseconds is one second.
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
WebElement usnfield = driver.findElement(By.id("email"));
usnfield.sendKeys("alex");
try {
Thread.sleep(7000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
WebElement pwdfield = driver.findElement(By.id("pass"));
pwdfield.sendKeys("alex42");
try {
Thread.sleep(7000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
WebElement lgn_btn = driver.findElement(By.id("u_0_8"));
lgn_btn.click();
}
}
public class facebookClass {
static WebDriver driver;
@Test(priority = 0)
public static void main(String[] args) {
loginDetailsClass.browserLaunch(); //function called from loginDetailsClass.java page
try {
Thread.sleep(7000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
@Test(priority = 1)
public static void searchfriends() {
//Switching to iFrame for locating elements
driver.switchTo().frame(driver.findElement(By.id("contentFrame")));
try {
Thread.sleep(7000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
//Searching a friend using friend name field
WebDriverWait wait = new WebDriverWait (driver, 20);
WebElement friendName = wait.until(ExpectedConditions.elementToBeClickable(By.id("txtFriendName")));
friendName.sendKeys("James");
try {
Thread.sleep(10000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
WebElement clck_Search = driver.findElement(By.id("btnSearch"));
clck_Search.click();
try {
Thread.sleep(7000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
WebElement select_friend = driver.findElement(By.xpath("//*[@id=\"trFriend_3\"]"));
select_friend.click();
try {
Thread.sleep(7000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
//Switching back to main frame from iFrame
driver.switchTo().defaultContent();
try {
Thread.sleep(7000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
@Test(priority = 2)
public static void logout() {
//Logout
WebElement logout = driver.findElement(By.xpath("/html/body/form/table/tbody/tr[1]/td/div/div[8]/table/tbody/tr/td[2]"));
logout.click();
try {
Thread.sleep(7000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
driver.close();
}
}
第二个文件是facebookClass.java,下面是文件中的代码
public class loginDetailsClass {
@SuppressWarnings("deprecation")
public static void browserLaunch() {
WebDriver driver;
String baseurl = "https://www.facebook.com/";
System.setProperty("webdriver.chrome.driver","D:\\eclipse-workspace\\chromedriver.exe");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.switches", Arrays.asList("--incognito"));
ChromeOptions options = new ChromeOptions();
options.addArguments("--test-type");
options.addArguments("--disable-extensions");
options.addArguments("--disable-infobars");
capabilities.setCapability("chrome.binary","D:\\eclipse-workspace\\chromedriver.exe");
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
driver = new ChromeDriver(capabilities);
driver.manage().deleteAllCookies();
driver.manage().window().maximize();
driver.get(baseurl);
//Login section
try {
Thread.sleep(7000); // 1000 milliseconds is one second.
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
WebElement usnfield = driver.findElement(By.id("email"));
usnfield.sendKeys("alex");
try {
Thread.sleep(7000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
WebElement pwdfield = driver.findElement(By.id("pass"));
pwdfield.sendKeys("alex42");
try {
Thread.sleep(7000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
WebElement lgn_btn = driver.findElement(By.id("u_0_8"));
lgn_btn.click();
}
}
public class facebookClass {
static WebDriver driver;
@Test(priority = 0)
public static void main(String[] args) {
loginDetailsClass.browserLaunch(); //function called from loginDetailsClass.java page
try {
Thread.sleep(7000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
@Test(priority = 1)
public static void searchfriends() {
//Switching to iFrame for locating elements
driver.switchTo().frame(driver.findElement(By.id("contentFrame")));
try {
Thread.sleep(7000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
//Searching a friend using friend name field
WebDriverWait wait = new WebDriverWait (driver, 20);
WebElement friendName = wait.until(ExpectedConditions.elementToBeClickable(By.id("txtFriendName")));
friendName.sendKeys("James");
try {
Thread.sleep(10000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
WebElement clck_Search = driver.findElement(By.id("btnSearch"));
clck_Search.click();
try {
Thread.sleep(7000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
WebElement select_friend = driver.findElement(By.xpath("//*[@id=\"trFriend_3\"]"));
select_friend.click();
try {
Thread.sleep(7000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
//Switching back to main frame from iFrame
driver.switchTo().defaultContent();
try {
Thread.sleep(7000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
@Test(priority = 2)
public static void logout() {
//Logout
WebElement logout = driver.findElement(By.xpath("/html/body/form/table/tbody/tr[1]/td/div/div[8]/table/tbody/tr/td[2]"));
logout.click();
try {
Thread.sleep(7000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
driver.close();
}
}
在执行facebookClass.java时,正确调用了login方法,并且登录成功。但从publicstaticvoidsearchfriends()方法来看,执行无法进一步工作。在最初的几天里,所有方法都成功地执行了。但从第二天起,它就开始工作了
searchfriends()
不是从任何地方调用的,对吗?searchfriends()没有被调用。您需要调用该方法才能调用该方法。看起来您正在混合两种模式来执行。@Test
注释显然是为测试框架准备的,而静态void Main(String[]args)
是可运行应用程序的标准模式。也许在过去,您使用测试运行程序执行此程序,而今天您直接作为应用程序执行它?@StriplingWarrior:我每次都使用selenium Oxygen.3a发行版(4.7.3a)运行测试。现在已经一个多星期了,我尝试了所有可能的方法来解决这个问题。但现在看来,我需要帮助。