Edge extension executeScript doesn't have result? - microsoft-edge-extension

I want to get the selected text in Edge, but the result is always empty
browser.tabs.executeScript({code: "window.getSelection().toString();"}).then(result => {
alert(result);
});

Seems to be a known bug - see
https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/8504764/

Related

cypress .next at end of list yields '' but I need to break instead

I am writing Cypress tests for an application that has a dynamic group/list of items. Each item has a details link that when clicked creates a popup with said details. I need to close that popup and move to the next item in the group.
I first tried to use the .each() command on the group and then would .click({multiple:true}) the details but the popup would cover the the next click. Adding {foce:true} does allow all of the popups to display but I don't think that is in the spirit of how the application should function.
My latest attempt has been to create a custom command using .next() to iterate through the group. This works but when .next() reaches the end of the group it yields "" and so the test ultimately fails.
The actual error I get is:
Expected to find element: ``, but never found it. Queried from element: <div.groups.ng-star-inserted>
the .spec.ts
describe('Can select incentives and view details', () => {
it('Views incentive details', () => {
cy.optionPop('section#Incentives div.groups:first')
})
})
the index.ts
Cypress.Commands.add('optionPop', (clickable) => {
cy.get(clickable).find('[ng-reflect-track="Estimator, open_selection_dial"]').click()
cy.get('mat-dialog-container i.close').click()
cy.get(clickable).next().as('clicked').then(($clicked) => {
//fails at .next ^ because '' is yielded at end of list
cy.wrap($clicked).should('not.eq','')
})
cy.optionPop('#clicked')
})
You basically have the right idea, but it might work better in a plain JS function rather than a custom command.
function openPopups(clickables) {
if (clickables.length === 0) return // exit when array is empty
const clickable = clickables.pop() // extract one and reduce array
cy.wrap(clickable)
.find('[ng-reflect-track="Estimator, open_selection_dial"]').click()
cy.get('mat-dialog-container i.close')
.should('be.visible') // in case popup is slow
.click()
// wait for this popup to go, then proceed to next
cy.get('mat-dialog-container')
.should('not.be.visible')
.then(() => {
openPopups(clickables) // clickables now has one less item
})
}
cy.get('section#Incentives div.groups') // get all the popups
.then($popups => {
const popupsArray = Array.from($popups) // convert jQuery result to array
openPopups(popupsArray)
})
Some extra notes:
Using Array.from($popups) because we don't know how many in the list, and want to use array.pop() to grab each item and at the same time reduce the array (it's length will control the loop exit).
clickables is a list of raw elements, so cy.wrap(clickable) makes the individual element usable with Cypress commands like .find()
.should('be.visible') - when dealing with popup, the DOM is often altered by the click event that opens it, which can be slow relative to the speed the test runs at. Adding .should('be.visible') is a guard to make sure the test is not flaky on some runs (e.g if using CI)
.should('not.be.visible').then(() => ... - since you have some problems with multiple overlapping popups this will ensure each popup has gone before testing the next one.

Cypress find element from multiple elements based on text and click on it

Cypress: I want to find an element from the list of an element based on text and want to click on that
cy.get('partie-room-list-item > div > div.content-block > span.partie-title').each(($elm , i) => {
cy.get($elm).invoke('text').then((text) => {
if (text === 'textinelement') {
expect(text.trim()).equal('textinelement').click();
cy.log(text)
}
})
});
cypress find element and click
https://www.screencast.com/t/p0rL6qexD5
Could you try below code and let me know the outcome. You could used .text() to get the text from the span element.
cy.get('partie-room-list-item > div > div > .partie-title').each(ele => {
const eleText = "cypresssendfollowRequest";
if (ele.text() === eleText) {
ele.click();
}
});
I am just guessing here. If you post your HTML I might be able to help you
cy.get('.partie-title').contains('textinelement').click()
looking at your image it would be like this
cy.get('.partie-title').contains('cypresssendfollowRequest').click()
it("This test is for selecting a single element based on text",()=>{
cy.visit('http://automationpractice.com/index.php')
cy.get("[title='Women']").each(($element, index)=>{
console.log("Object is "+ $element.text())
if($element.text()=='Women'){
cy.get($element).click()
}
})
})
or
it("This test is for selecting a single element based on text",()=>{
cy.visit('http://automationpractice.com/index.php')
cy.get("[title='Women']").contains('Women').click()
})
You want to use cy.contains() (see documentation).
For example:
cy.contains('textinelement').click();
You can also add specificity so it only looks for text within a specified selector:
cy.contains('.partie-title', 'textinelement').click();

How to check all the web-element in the list are displayed

I want to verify 'About Us', 'Contact Us' and 'FAQ' text links under 'More Information' footer section are displayed or not
How I can check it? Do I have to get xpath of each list web-element and then check whether its displayed on the web page? or lists of all web-elements at once,and then trace through for loop?
And also help me in writing xpath for 'About Us' element
You can verify all at once or you can verify individually also, try the below code which will fetch all the options, verifies whether those are present or not and checks they are displayed or not:
// Get all the options using the below line
List<WebElement> elements = driver.findElements(By.xpath("//div[#class='footer-section']//a"));
// Check they are present or not?
if(elements.size() > 0) {
System.out.println("=> The Options are present...");
} else {
System.out.println("=> The Options are nor present...");
}
// Check they are displayed or not?
for(WebElement element : elements) {
System.out.println(element.getText()+" is displayed? "+element.isDisplayed());
}
If you want to verify individually then you need to do like below:
driver.findElement(By.xpath("//div[#class='footer-section']//a[text()='About Us']")).isDisplayed();
If you want to verify that each element is present or not then yes you need to check separately for every element. Though you can make one generic method to check its present or not using assert.
For example:
public void checkElementPresent(String elementText){
Assert.assertTrue(driver.findElements(By.xpath("//a[text()="+elementText+"]")).size()>0);
}
Or if you dont want to use assert then you can use if else condition as well, like:
public void checkElementPresent(String elementText){
if(driver.findElements(By.xpath("//a[text()="+elementText+"]")).size()>0){
System.out.println("Element is present");
}else{
System.out.println("Element is not present");
}
}
And you can just call the method by sending the text of the link you want to verify if its present or not:
checkElementPresent("About Us"); or
checkElementPresent("Contact Us");

How to set cookies within Cypress testing?

this example taken from the Cypress documentation won't work properly:
cy.getCookies().should('be.empty');
cy.setCookie('session_id', '189jd09sufh33aaiidhf99d09');
cy.getCookie('session_id').should('have.property', 'value', '189jd09sufh33aaiidhf99d09');
Every time I try to setCookie(), it appears to set it without issue but always returns this when I call getCookies():
$Chainer {chainerId: "chainer18", firstCall: false}
chainerId: "chainer18"
firstCall: false
__proto__: Object
Is there something I'm missing here?
You should use cy.getCookies() without arguments. That function returns array of cookies that you have. Than you can do:
cy.getCookies().should('have.length', 1).then((cookies) => {
expect(cookies[0]).to.have.property('session_id', '189jd09sufh33aaiidhf99d09')
})
Looks like this was related to https://github.com/cypress-io/cypress/issues/1321 and has been patched in v 3.1.2 of Cypress. All is right with the world again.

Ignore or replace Handlebars blocks that don't compile

In a handlebars template in Ember.js, I have blocks like the following:
{{content.some_attribute}}
{{content.some_other_attr}}
{{content.more_attr}}
Some of these attributes don't exist and I'm implementing them slowly.
Is there a way to get these templates to compile and either ignore the blocks that don't evaluate or better yet, replace them with a html element so they're easier to spot in the browser?
(the template is pretty large and it's being converted from ERB slowly,
Is there a way to get these templates to compile and either ignore the blocks that don't evaluate
Properties that don't exist are undefined, and don't get rendered at all. In other words {{thisDoesNotExist}} will simply be invisible -- it will compile just fine.
or better yet, replace them with a html element so they're easier to spot in the browser
As Cory said, you could use a helper for this that checks for undefined, using Ember.Handlebars.registerBoundHelper.
This seems like a perfect case for a handlebars helper. The helper could validate the value and return the value or the html that you desire.
The following code should be used very carefully, since it has not been tested within an application.
A possible solution to replace the possible undefined value in a template is to overwrite Ember.getPath, which is used to lookup the value of a path in a template, see http://jsfiddle.net/pangratz666/hKK8p/:
var getPath = Ember.getPath;
Ember.getPath = function(obj, path) {
var value = getPath(obj, path);
return (Ember.none(value) ? 'OMG %# is not defined!!'.fmt(path) : value);
};
If this code would be used temporarily in an application, I would also restrict the check for undefined values to a specific obj. So something along those lines:
App.objectWhichHasUndefinedProps = Ember.Object.create({
...
});
Ember.View.create({
templateName: 'templateWithAttributes',
objBinding: 'App.objectWhichHasUndefinedProps'
}).append();
var getPath = Ember.getPath;
Ember.getPath = function(obj, path) {
var value = getPath(obj, path);
if (obj === App.objectWhichHasUndefinedProps) {
return (Ember.none(value) ? 'OMG %# is not defined!!'.fmt(path) : value);
}
return value;
};