#BeforeMethod
public void setup ()
{
System.setProperty("webdriver.chrome.driver", "src\\test\\resources\\chromedriver.exe");
driver = new ChromeDriver(); // Opens Chrome browser
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
//driver.manage().window().maximize();
}
#AfterMethod
public void tearDown() throws Exception
{
Thread.sleep(5*1000);
driver.quit();
}
#Test
public void browserCommandsTests()
{
try {
driver.get("http://www.costco.com");
Thread.sleep(3 * 1000);
driver.navigate().to("http://www.facebook.com");
Thread.sleep(3 * 1000);
driver.navigate().back();
Thread.sleep(3 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
When i'm testing this code after opening the urls it doesn't closes the
testing browser.
it opens only 1 link and closes browser when i'm getting only 1 url. but as soon as i try to get 2nd url it opens 2nd url and after it doesn't closes the browser under test.
It is your tag #AfterMethod change it to AfterClass (also you can try close instead of quit):
#AfterClass
public void tearDown() throws Exception
{
driver.close();
}
Also, friendly advice, using the thread.sleep will cause you more grief than joy. The alternative is to use driver.findElement() in a try/catch and depending on the result (true/false) direct your flow from there.
Best of luck!
#AfterMethod
public void tearDown() throws Exception
{
driver.quit();
}
Related
My JUnit Tests run fine in eclipse and pass locally however in Jenkins my 1st test passes and 2nd one fails and I know why but cant figure out the solution. The Jenkins output says it cant find the element however it is there and I have the proper waits.
Here is the 1st test case that passes.
#Before
public void setUp() throws Exception {
System.setProperty("webdriver.chrome.driver", "C:\\eclipse\\chromedriver\\chromedriver.exe");
driver = new ChromeDriver();
homeUrl = "https://myadp.adpcorp.com/wps/myportal/main/myADP_new";
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get(homeUrl);
}
//Full test
#Test
public void fullTest() throws Exception {
step01_VerifyHomePage();
}
// Go to Home page and verify title
public void step01_VerifyHomePage() throws Exception {
driver.getTitle().contains(homeTitle);
Thread.sleep(3000);
}
#After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
This is the one that fails in Jenkins
//Full test
#Test
public void fullTest() throws Exception {
step02_HoverMyWorkSpace();
}
public void step02_HoverMyWorkSpace() throws Exception {
WebDriverWait wait = new WebDriverWait(driver,20000);
wait.until(ExpectedConditions.visibilityOfElementLocated((By.id("DivMyWorkList"))));
if ( driver.findElement(By.id("DivMyWorkList")).isDisplayed()){
System.out.println("DivMyWorkList Visiable");
}else{
System.out.println("DivMyWorkList NOT Visiable");
}
// Assert.assertEquals(true, workspace.isDisplayed());
// Thread.sleep(3000);
}
#After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
Jenkins Output
The problem i think is that Jenkins cant find the element because the page is not coming up in what ever environment it is using. When I use
wait.until(ExpectedConditions.visibilityOfElementLocated((By.id("DivMyWorkList")))); Jenkins test hangs and times out.
If this is the case is there a way around this? It dose not make sense that the 1st test passes by using the same url and the 2nd one uses same url to the exact same page and it can find the title but not the web element.
I am using a VM and created a free style project on it.
I have been working on this for the past week any help would be appreciated.
Below is my SAMPLE code in which i am trying to create a simple report using the Leanft in which i am getting on result xml file.
#Test
public void Google() throws Exception {
Reporter.init();
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.com");
Thread.sleep(4000);
if( driver.getTitle().equalsIgnoreCase("google")){
Reporter.reportEvent("test", "test",Status.Failed);
}
Reporter.generateReport();
driver.quit();
}
As specified in the docs, if you're going to use a custom framework you'll also need to initialize the SDK (SDK.init() and SDK.cleanup())
E.G.
public static void main(String [] args){
// initialize the SDK and report only once per process
try{
ModifiableSDKConfiguration config = new ModifiableSDKConfiguration();
config.setServerAddress(new URI("ws://myServerAddress:5095"));
SDK.init(config);
Reporter.init();
//put your test code here.
//Generate the report and cleanup the SDK usage.
Reporter.generateReport();
SDK.cleanup();
} catch(Exception e){
}
}
I see nothing wrong with your code as the report generates as you told it too.
However, I believe you were wanting something more like this to show that it PASSES when it finds the Google title:
#Test
public void Google() throws Exception {
Reporter.init();
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.com");
Thread.sleep(4000);
if( driver.getTitle().equalsIgnoreCase("google")){
Reporter.reportEvent("test", "test",Status.Passed);
} else {
Reporter.reportEvent("test","test",Status.Failed);
}
Reporter.generateReport();
driver.quit();
}
I am trying run the below mentioned code, its not working , everytime a new window gets open instead of closing the current window and opening a window.
public class Next2beforendafter {
public static WebDriver driver;
#BeforeMethod
public void launchBrowser() {
System.setProperty("webdriver.gecko.driver", "C:/Users/xyz/Videos/selenium/geckodriver-v0.11.1-win64/geckodriver.exe");
driver = new FirefoxDriver();
}
#Test(priority = 1)
public void verifygoogleTitle() {
driver.get("http://www.google.com");
Assert.assertEquals("Google", driver.getTitle());
}
#Test(priority = 2)
public void verifyyahooTitle() {
driver.get("https://in.yahoo.com");
Assert.assertEquals("Yahoo", driver.getTitle());
}
#Test(priority = 3)
public void verifybankofindiaTitle() {
driver.get("http://www.bankofindia.co.in/english/home.aspx");
Assert.assertEquals("Bank Of India - Home", driver.getTitle());
}
#AfterMethod
public void closeBrowser() {
driver.close();
}
}
Selenium Ver.3.0.1
You should be using driver.quit() if you want to close the webdriver. driver.close() only closes the current window (tab) but leaves the driver session open
i have tried to execute multi logins(using TestNG), my first test1 is passed but remaining tests gives some error like "Element is not currently visible and so may not be interacted with Command duration or timeout: 28 milliseconds". Kindly resolve my issue
String baseUrl = "******************";
WebDriver driver= new FirefoxDriver();
#BeforeTest
public void b_url()
{
driver.get(baseUrl);
}
#Test(priority=1)
public void login1() throws InterruptedException
{
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.findElement(By.xpath("//*[#id='w1']/li/a")).click();
driver.findElement(By.id("LoginFormusername")).sendKeys("Boopathi");
driver.findElement(By.id("LoginForm-password")).sendKeys("test");
driver.findElement(By.xpath("//*[#id='login-form']/div/div/div/div/div[3]/input")).click();
Thread.sleep(3000);
driver.findElement(By.xpath("//*[#id='w6']/li/a")).click();
driver.findElement(By.xpath("//*[#id='w7']/li[3]/a")).click();
//driver.navigate().refresh();
}
#Test(priority=2)
public void login2() throws InterruptedException
{
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.findElement(By.xpath("//*[#id='w1']/li/a")).click();
driver.findElement(By.id("LoginForm-username")).sendKeys("john");
driver.findElement(By.id("LoginForm-password")).sendKeys("test");
driver.findElement(By.xpath("//*[#id='login-form']/div/div/div/div/div[3]/input")).click();
Thread.sleep(6000);
driver.findElement(By.xpath("//*[#id='w6']/li/a")).click();
driver.findElement(By.xpath("//*[#id='w7']/li[3]/a")).click();
driver.navigate().refresh();
}
#Test(priority=3)
public void login3() throws InterruptedException
{
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.findElement(By.xpath("//*[#id='w1']/li/a")).click();
driver.findElement(By.id("LoginForm-username")).sendKeys("Kamal");
driver.findElement(By.id("LoginForm-password")).sendKeys("test");
driver.findElement(By.xpath("//*[#id='loginform']/div/div/div/div/div[3]/input")).click();
driver.navigate().refresh();
Thread.sleep(6000);
driver.findElement(By.xpath("//*[#id='w6']/li/a")).click();
driver.findElement(By.xpath("//*[#id='w7']/li[3]/a")).click();
}
#AfterTest()
public void logout()
{
driver.quit();
}
Navigate application each time
#BeforeMethod
public void b_url()
{
driver.get(baseUrl);
}
Add refresh code in each test after deleting cookies
driver.navigate().refresh();
Let me know this will not work
Hello i really need help with Selenium WebDriver using TestNG and Excel
i try to get data from excel to open browser and navigate URL. its work successfully and terminal and testng report showing test pass but its not open browser or doing anything its just run its self and show report
Config File
public void openBrowser(String browser){
try {
if (browser.equals("Mozilla")) {
driver = new FirefoxDriver();
} else if(browser.equals("IE")){
driver = new InternetExplorerDriver();
} else if(browser.equals("Chrome")){
System.setProperty("webdriver.chrome.driver", "\\Applications\\Google Chrome.app\\Contents\\MacOS\\Google Chrome ");
driver = new ChromeDriver();
}
} catch (Exception e) {
}
}
public void navigate(String baseUrl){
try {
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.navigate().to(baseUrl);
} catch (Exception e) {
}
}
And Test Execute File
public class NewTest {
public String exPath = Config.filePath;
public String exName = Config.fileName;
public String exWrSheet = "Logiin Functional Test";
public Config config;
#BeforeTest
public void openBrowser() {
config = new Config();
Excel.setExcelFile(exPath+exName, exWrSheet);
String browser = Excel.getCellData(1, 2);
config.openBrowser(browser);
}
#BeforeMethod
public void navigate() {
config = new Config();
Excel.setExcelFile(exPath+exName, exWrSheet);
String baseUrl = Excel.getCellData(2, 2);
config.navigate(baseUrl);
}
#Test
public void test() {
System.out.println("Test");
}
#AfterTest
public void closeBroser() {
//Config.tearDown();
}
I don't have quite enough rep to make a comment, which I would prefer here, but if you aren't getting any sort of exception, I'd suspect the value you're getting for the browser variable is not matching anything in your if-then-else in openBrowser and then falling through. Step through the code with a debugger or just add:
String browser = Excel.getCellData(1, 2);
System.out.println("Browser value from Excel =" + browser);
config.openBrowser(browser);
to see what you're reading from the file.
1 - TestNg is always pass because you are using "void" method and you catch "all" exception
2 - No browser opened because in openBrowser(String browser), NullPointException throws and you already catch it.
-> you need to init an instance of WebDriver and pass it through your test.