PHP, Codeception, Firefox and new tab - how to? - selenium-webdriver

I have a test for PHP project which opens a new tab by double-click. I test it in Firefox.
I am new to Codeception, but I've read and tried everything I've found with no luck.
I have a table which rows are spans. I want to double-click it and to check if correct page is open.
I try:
$i->scrollTo(['xpath' => '//span[text()="test"]']);
$i->doubleClick(['xpath' => '//span[text()="test"]']);
$i->waitForText("test text", 15);
$i->seeCurrentUrlMatches('~^/register/materials/view/\d+$~');
The issue is that doubleClick opens a requested page in new tab like this:
jumpTo: function(row) {
const link = document.createElement('a');
link.href = window.location.pathname + '/view/' + row.id;
link.setAttribute('target', '_blank');
document.body.appendChild(link);
link.click();
},
If I try to run this test as is - it starts, double-clicks span, then new tab opens, focus switches to it, I see correct page and test text on it, but test fails because it does not see requested text because it seems that though I see that tab changed the WebDriver remains on first tab.
I tried switchToNextTab() - it does not work. Sometimes switchToNextTab() 3 times in a row works, sometimes not.
I tried reloadPage() - it reloads first tab, not the one recently opened.
I tried
$I->executeInSelenium(function (\Facebook\WebDriver\Remote\RemoteWebDriver $webdriver) {
$handles=$webdriver->window_handles();
$last_window = end($handles);
$webdriver->focusWindow($last_window);
});
and it seems that there is one currently active window in terms of this method ($handles is an array of 1 element).
I tried closeTab() - it closes entire Firefox instance.
How can I programmatically change active tab?

Related

Python-selenium action chains not working in Safari

I am trying to click on a menu and then a submenu in selenium-python on a Safari Webdriver. No matter what I do, I cannot seem to make the ActionChains do anything whatsoever. Am I doing something wrong or is this an issue with Safari?
I have tried a number of different actions using the ActionsChain, but none of them seem to be working
```driver = webdriver.Safari()
wait = WebDriverWait(driver, 20)
url = "someurl.com"
link_text = "link text"
driver.get(url)
driver.maximize_window()
wait.until(EC.element_to_be_clickable((By.LINK_TEXT, link_text)))
ActionChains(driver).move_to_element(driver.find_element(By.LINK_TEXT, link_text)).click().perform()
print('Hello World')```
I expect to see the browser clicking on the element, but I see only the terminal of my program printing 'Hello World'.
You could try clicking with JavaScript instead of ActionChains here:
element_to_click = wait.until(EC.element_to_be_clickable((By.LINK_TEXT, link_text)))
driver.execute_script(“arguments[0].click();”, element_to_click)

Protractor - Unable to access element due to fixed Top navigation bar

I'm facing the following issue in protractor with jasmine
Click/mouse hover not working because of fixed top navigation bar in my application. I need to click/perform mouse hover on a web page.
Unfortunately that element is displaying behind that fixed navigation bar. So scroll till element present & click by x & y coordinates are not working.
My dependencies are :
protractor version 5.2.2
node 8.9.3
selenium standalone 3.13
chrome driver-2.40
chromebrowser v67
OS- Windows 10
Thanks in advance
Try using prototype executeScript
Just try clicking that element from the browser console using id,name or xpath.
For example :
var el = element(by.module('header'));
var tag = browser.executeScript('return arguments[0].click()', el).then(function() {
expect(something).toMatch(something);
});
Another way, along the same lines as what Bharath Kumar S and knowing JeffC's caveat that this approach is cheating, I had a similar issue where the App-Header kept getting in my way of clicking, and I knew I was willing to never need it (so, for instance, to find other ways to navigate or log out and not check for stuff that was on it). I, therefore, did the following, which solved the problem. Note if you refresh the screen, you have to call it again. Also note I am using a number of functions from https://github.com/hetznercloud/protractor-test-helper, which do what you would expect from their names.
var removeAppHeaderIfAny = async function() {
//this function hides the app header
//it is useful to avoid having covers there when Protractor worries that something else will get the click
let found = false;
try {
found = await waitToBeDisplayed(by.className("app-header"), 2000);
} catch (e) {
let s: string = "" + e;
if (s.search("TimeoutError") != 0) flowLog("presumably fine, cover already removed: " + e);
found = false;
}
if (!found) return;
if (found) {
let coverElement = await element(by.className("app-header"));
browser.executeScript(
"arguments[0].style.visibility='hidden';",
coverElement
);
await waitToBeNotDisplayed(by.className("app-header"), 10000);
}
return;
//note after this is called you will not see the item, so you cannot click it
};
As I look at the code, it strikes me one can probably remove the if (found) and associated brackets at the end. But I pasted in something I know has been working, so I am not messing with that.
As indicated up front, I knew I was willing to forego use of the app-header, and it is a bit crude.

How to close file upload dialogue after click on upload button in protractor

I want to upload an image file in protractor. the problem is i can not get "input[type = "file"]" element without clicking on upload button.
when I click upload button, file upload dialogue popup opens.
i have tried
browser.actions().sendKeys(protractor.Key.ESCAPE).perform(); but it does not work.
This is what I am doing:
var image = '../images/image1.png';
var absPathImg = path.resolve(__dirname, image);
element(by.id('uploadImage')).click();
browser.actions().sendKeys(protractor.Key.ESCAPE).perform();
element(by.css('input[type=file]')).sendKeys(absPathImg);
element(by.id('upload')).click();
How can i close that file upload dialogue to upload an image?
Try it like this. I used this for Protractor, but I think it's similar for Selenium.
I also have a problem for Windows File dialog not closing.
This is how I solved that:
element(by.css('elementForOpenDialog')).sendKeys(absPathImg);
Send Keys to the element which will open file dialog, not click on element to open file dialog. Just send to that element absolute path and it will upload without opening dialog.
It's worked for me. Good Luck!
This is an old thread but I'll answer in case anyone else runs to this problem.
I had to solve this same problem today. I was able to work around opening the upload dialog, because I am quite sure it cannot be closed without some external libraries and that would not work in headless mode.
You need to inspect the javascript linked to the upload button's click event and execute some script before sending keys to the file input.
This is individual to each case, but in my case i had to do this (Python):
script = (
"var url = '/Contacting/UploadCsv'; "
"$('#buttonFileUpload').data('url', url);"
)
driver.execute_script(script)
driver.find_element_by_id('buttonFileUpload').send_keys(csv_path)
I picked that script from the JS function linked to the button.
I landed here because I was searching on HOW TO CLOSE AN IMAGE WHEN CLICKED
In case that helps someone.
So what I did.
var Img = Driver.FindElement(XPath.News.YourImg);
Img.Click();
try
{
Img.Click();
Assert.IsFalse(feedImg.Displayed);
}catch(Exception ex)
{
}
This way you can check if the image is closed by trying to click on it again.
Instead of clicking on to ESCAPE, why not simulate the ENTER key press to simulate a person entering the path and pressing the ENTER key to upload the image.
I am doing this in my code and this works perfectly fine.
var path = require('path')
this.fileUploadOne = element(by.css('locator_to_element'));
var fileUpload = 'path_of_file_to_upload'
absolutePath = path.resolve(__dirname,fileUpload);
console.log(absolutePath);
this.fileUploadOne.sendKeys(absolutePath);
browser.actions().sendKeys(protractor.Key.ENTER).perform();
//just for debugging purposes.
browser.sleep(20000);

$location.hash() - Not running rest of JS. Weird behavior?

Trying to do a simple scroll to the top of my page when the user clicks the 'edit' button. For some reason, when I try to set the $location.hash, it will not run any code following it.
It will put the value into the console, and in the browser address bar, but nothing after.
If you click 'edit' again, (the location value will be in the address bar, i.e. - sitename.com/#/send#top), and the rest of my code will run as expected.
Any ideas what I am doing wrong?
scrollToLoc = function(loc) {
$location.hash(loc);
$anchorScroll();
console.log(loc);
};
$scope.doEdit = function(item) {
scrollToLoc('top');
$scope.request = item;
};

Opened file gets cached in titanium. Cant view replaced file content while app is running

I am facing a slight issue while editing a file.
I am saving a file (image) with a certain name on ios using the code below.
But the problem is that when i replace the image stored in a file (eg temp.jpg) the app still picks up the previous image when i open the file. However the new image can be seen in the explorer.
If i restart the app the new image appears while opening the image.
var folder = Ti.Filesystem.getFile(Ti.Filesystem.externalStorageDirectory, 'DocImages');
// DocImages is a folder for files
// ImageVar contains the blob for the new image from camera or gallery
// docImgModel is the model class containing the name and image path of the image
if(imageVar !== null && imageVar !== undefined){
if (docImgModel.imagePath !== null && docImgModel.imagePath !== undefined){
tempFile = Ti.Filesystem.getFile(docImgModel.imagePath);
if (tempFile.exists()) {
tempFile.deleteFile(); // deleting already existing file
}}
// in case of changing the image stored in a file(the case in which i have a
// problem) the imgFile(below) is same as docImgModel.imagePath (above)
imgFile = Ti.Filesystem.getFile(Ti.Filesystem.externalStorageDirectory + 'DocImages/', filenameWithExtension); // creating a new file
// writing image to file
imgFile.write(imageVar);
Ti.API.info('new image saved');
}
}
I was wondering if titanium saves cache of the file already opened and is hence not able to show the new image.
If not, is there anything else i am doing wrong or something i could do to make it work.
I just want to show the new saved image. Is there any way to do it.
Thanks.
I haven't worked with opening files from the device, but I ran into a similar issue when trying to update data on my screen. If you are saying when you open the app and it loads the correct image comes up, then the code that you use to load the image appears correct and working. I assume that is the code you posted above. Even that appears to be a code fragment of a more complete file.
You didn't post any UI code, which is probably where your real problem is coming from. You have an object, a view of some sort I'm guessing, that is already rendered using the old image before you load the new image. So debugging, you might see the new image's data loaded in the code above, but the UI element hasn't been assigned or updated correctly.
As a test, I would suggest that you put some test code into your app that allows you to destroy your UI elements and recreate them, you will probably see the picture come up properly in that case.
According to this post: http://developer.appcelerator.com/question/31181/simple-image-refresh
Just assigning the image you loaded to the url of the image should update it. Your example code doesn't show the image object that you are attempting to update and how that communication is made from the code that is loading the image.
// this sets the background color of the master UIView (when there are no windows/tab groups on it)
Titanium.UI.setBackgroundColor('#000');
// create tab group
var tabGroup = Titanium.UI.createTabGroup();
var image1 = 'image1.png';
var image2 = 'image2.png';
//
// create base UI tab and root window
//
var win1 = Titanium.UI.createWindow({
title:'Tab 1',
backgroundColor:'#fff'
});
var tab1 = Titanium.UI.createTab({
icon:'KS_nav_views.png',
title:'Tab 1',
window:win1
});
var img = Titanium.UI.createImageView({
width:100,
height:100,
top:50,
left:110,
url:image1
});
win1.add(img);
var btn = Titanium.UI.createButton({
title:'load',
width:100,
height:35,
top:50,
left:0
});
function switchImage(){
if(img.url == image1){
img.url = image2;
} else {
img.url = image1;
}
}
btn.addEventListener('click',function(){
switchImage();
});
setInterval(switchImage,3000);
win1.add(btn);
//
// add tabs
//
tabGroup.addTab(tab1);
// open tab group
tabGroup.open();

Resources