Test Upload File In Selenium - java

Currently i wish to run selenium for test excel upload. And my coding didn't working as expected. Just wondering how to resolve it. Every time i run the automated testing my code will be stop at this step:sendKeys("C:\Users\user\Desktop\JSPL Style Excel Upload.csv") without any error message show. Any suggestion?
#Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "http://localhost:7101/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
public void testJava() throws Exception {
driver.get(baseUrl + "SCM_Telephone_Accounting_2-ViewController-context-root/faces/Main_Menu");
driver.findElement(By.id("np2:cni10")).click();
driver.findElement(By.id("r1:1:if1::content")).click();
driver.findElement(By.id("r1:1:if1::content")).clear();
driver.findElement(By.id("r1:1:if1::content")).sendKeys("C:\\Users\\user\\Desktop\\JSPL Style Excel Upload.csv");
driver.findElement(By.id("r1:1:cb1")).click();
}

Related

JUnit Test Fails in Jenkins

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.

Leanft Custom Framework HTML reports are not generated

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();
}

Able to locate frame with selenium, but doesn't work with Junit Cucumber

I am able to locate a frame using a selenium java program. The following code runs successfully:
public static void TestCase() throws IOException, InterruptedException{
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("website");
driver.findElement(By.xpath("//*[#id='username']")).sendKeys(userName);
driver.findElement(By.xpath("//*[#id='password']")).sendKeys("password");
driver.findElement(By.xpath("//*[#id='session_link']")).click();
driver.findElement(By.xpath("//*[#id='app_236']/a")).click(); //Click a link on the page
driver.switchTo().frame("iframe100-100"); //Switch to a frame within the new page
driver.findElement(By.xpath("//*[#id='yui-gen0']/li[2]/div")).click(); //Click element within the frame
}
But if I run the same code using a Cucumber Junit maven project, the program is not able to locate the frame.
#Given("^I log into website$")
public void log_in()
{
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("website");
driver.findElement(By.xpath("//*[#id='username']")).sendKeys(userName);
driver.findElement(By.xpath("//*[#id='password']")).sendKeys("password");
driver.findElement(By.xpath("//*[#id='session_link']")).click();
}
#When("^I select the link from homepage$")
public void select_link() throws InterruptedException
{
driver.findElement(By.xpath("//*[#id='app_236']/a")).click();
}
#Then("^I should see something$")
public void click_element_within_frame()
{
driver.switchTo().frame("iframe100-100"); //Switch to a frame within the new page
driver.findElement(By.xpath("//*[#id='yui-gen0']/li[2]/div")).click(); //Click element within the frame
}
Error message :
31morg.openqa.selenium.NoSuchFrameException: Unable to locate frame: iframe100-100
Any suggestions ?

Selenium WebDriver using TestNG and Excel

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.

Timeout error with windowMaximize using Selenium RC

I am trying to navigate to a page and maximize and focus the page..My code is as below -.
public class DynamicTableHandling
{
public static void main(String[] args) throws InterruptedException
{
Selenium selenium = new DefaultSelenium("localhost",4444,"*firefox","http://scores.sify.com");
selenium.start();
selenium.open("/match/scorecard/ENG_IND_JUL27_JUL31_2014.shtml");
selenium.waitForPageToLoad("180000");
selenium.windowMaximize();
selenium.windowFocus();
}
}
It opens up the URL but doesn maximize or focus the window. I have been working on gmail and other websites and there both the windowMAximize() and windowFocus were working perfectly. Moreover I am getting error as below:
Exception in thread "main" com.thoughtworks.selenium.SeleniumException: Timed out after 30000ms
at com.thoughtworks.selenium.HttpCommandProcessor.throwAssertionFailureExceptionOrError(HttpCommandProcessor.java:109)
at com.thoughtworks.selenium.HttpCommandProcessor.doCommand(HttpCommandProcessor.java:103)
at com.thoughtworks.selenium.DefaultSelenium.open(DefaultSelenium.java:369)
at DynamicTableHandling.main(DynamicTableHandling.java:13)

Categories