Selenium脚本异常:main方法执行后后续测试方法未运行
Fixing Your TestNG Test Execution Issues
Let's break down why your searchfriends and logout methods aren't running, and fix the code step by step:
Key Problems in Your Current Code
- Invalid Method Nesting: You've defined
searchfriendsandlogoutinside themainmethod—Java doesn't allow nested methods. This is a syntax error that prevents TestNG from detecting these test cases. - Misused
@Testonmain: Themainmethod is a standard Java entry point, not a TestNG test method. Annotating it with@Testconfuses TestNG and isn't necessary. - Uninitialized
driver: Thedrivervariable infacebookClassis static but never assigned a value. ThebrowserLaunch()method creates a local driver instance that isn't passed back to your test class, leading to null pointer exceptions if tests did run. - Static Test Methods: TestNG works best with non-static test methods so it can properly instantiate your test class.
Corrected Code
loginDetailsClass.java
Modified to return the WebDriver instance so your test class can use it:
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.chrome.ChromeOptions; import org.openqa.selenium.remote.DesiredCapabilities; import java.util.Arrays; public class loginDetailsClass { @SuppressWarnings("deprecation") public static WebDriver 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(ChromeOptions.CAPABILITY, options); driver = new ChromeDriver(capabilities); driver.manage().deleteAllCookies(); driver.manage().window().maximize(); driver.get(baseurl); // Login section try { Thread.sleep(7000); } 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(); return driver; // Return driver instance to test class } }
facebookClass.java
Fixed method structure, driver initialization, and TestNG annotations:
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; public class facebookClass { WebDriver driver; @BeforeClass public void setup() { // Initialize driver once before all tests driver = loginDetailsClass.browserLaunch(); try { Thread.sleep(7000); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); } } @Test(priority = 1) public void searchfriends() { // Switch to iFrame driver.switchTo().frame(driver.findElement(By.id("contentFrame"))); try { Thread.sleep(7000); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); } // Search for friend using explicit wait 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(); // Switch back to main frame driver.switchTo().defaultContent(); try { Thread.sleep(7000); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); } } @Test(priority = 2) public 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.quit(); // Properly end the WebDriver session } }
How to Run the Tests
Instead of running the main method, right-click on facebookClass in Eclipse and select Run As > TestNG Test. This will execute the setup method first, then run searchfriends and logout in order of their priority.
Bonus Tip
Replace Thread.sleep() with explicit waits (like WebDriverWait) wherever possible—this makes your tests more reliable and less prone to timing issues.
内容的提问来源于stack exchange,提问作者Arvind
相关产品推荐
相关产品推荐

