Protractor, element not displayed or not present? - jasmine

Is there a way to test if an element is not present or not displayed ?
I have some elements that on some pages are not present and some other pages are just not displayed. How can i test this with only one instruction ?
Actually if i put :
expect(element.isDisplayed()).toBe(false)
it's ok when not displayed but crashes when not present.
And
expect(element.isPresent()).toBe(false)
it's ok when not present but crashes when present but not displayed...
Many thanks

You can create your own method.
public shouldBeVisible(elem: ElementFinder, shouldBe: boolean) {
if (!shouldBe) {
return elem.isPresent().then((isPres) => {
if (!isPres) {
return expect(elem.isPresent()).toBe(false);
}
return expect(elem.isDisplayed()).toBe(false);
});
}
return expect(elem.isDisplayed()).toBe(true);
}

Related

cypress: How can manage the application flow, if the element xpath is not present

I have the following scenario:
if the element is present, i have to do one activity and if not present will do another activity.
cy.xpath("//div[text()= 'button').its('length').then(res=> {
if (res > 0) {
return 1;
}
else {
cy.log ("Element is not present")
}
}
)} '''
if element is present = Code is working fine,
if the element xpath is not present = it try to search the element xpath (//div[text()= 'button') and throwing the error as 'Timed out retrying: Expected to find element: undefined, but never found it.'
if element is not present, Is there any way, i can handle the code ,
When using xpath you can (sort of) make it conditional by wrapping the xpath selector with count().
cy.xpath("count(//div[text()= 'button'])") // ok with async content
.then(count => {
if (count) {
//return 1; // not useful, returns a "chainer"
// ...but you can perform the required test here, e.g
cy.xpath("//div[text()= 'button']").click()
} else {
cy.log('not found')
}
})
The shorter syntax using built-in jQuery might be
const exists = !!Cypress.$("div:contains('button')").length
if (exists) {
cy.xpath("//div[text()= 'button']").click()
} else {
cy.log('not found')
}
Note that this is a partial match to 'button', where-as the xpath syntax is an exact match.
Also note - using Cypress.$ by-passes retry-ability, so it should not be used where the text is asynchronous.
From docs
This is a great way to synchronously query for elements when debugging from Developer Tools.
The implication is that it's more for debugging after the page has loaded.
The best practice is to try to construct the test and the app's data in such a way that you know that the button is present.
You can do something like this. With Cypress.$, you can validate the presence of the element with the help of the length attribute and then do further actions.
cy.get('body').then($body => {
const ele = $body.find('selector');
if (Cypress.$(ele).length == 0) {
//Do Something element is not present
}
else if (Cypress.$(ele).length > 0) {
//Do Something when element is present
}
})

Laravel Search Condition Problem. When condition is value empty, then go to else condition. But it's not work properly

This is my search code please see then solution, thank you.
You have to use like this
$report_ex = Expenses::where(condition)->where(condition)->get();
or
$report_ex = Expenses::where(condition)->where(condition)->first();
If you are using
$report_ex = Expenses::where(condition)->where(condition)->first();
then you need to call
if(!empty($report_ex))
{
// your code
}
else
{
//your code
}
But if you are using
$report_ex = Expenses::where(condition)->where(condition)->get();
then you should use
if(count($report_ex) > 0)
{
// your code
}
else
{
// your code
}
since get function returns an empty object

TornadoFX - how to fix ListView with a custom cell factory not updating properly when items are deleted?

I have a ListView displaying custom objects from my domain model, and if I use a custom cell factory to display the objects' properties in each row of the list, I get strange behaviour when I delete items. If the item is not the last in the list, the deleted item remains visible and the last item disappears. However, the item has been removed from the backing list as expected, and attempting to delete the phantom object has no further effect.
The display seems not to be refreshing properly, because after some arbitrary resizing of the window, the list eventually refreshes to its expected values. I've tried calling refresh() on the ListView manually but it has no noticeable effect.
Removing my custom cell factory fixes the problem, and I've seen other posts that have had a similar problem using standard JavaFX (ListView using custom cell factory doesn't update after items deleted) where the problem is fixed by changing the implementation of updateItem(Object item, boolean empty), but I can't work out how to do that in TornadoFX.
Here's an example that demonstrates the update issue (but not the phantom item, that only happens if the delete button is part of the custom cell):
package example
import javafx.scene.control.ListView
import tornadofx.*
data class DomainClass(val name: String, val flag1: Boolean, val flag2: Boolean, val info: String)
class UpdateIssue : App(UpdateIssueView::class)
class UpdateIssueView : View() {
val listSource = mutableListOf(
DomainClass("object1", true, false, "more info"),
DomainClass("object2", false, true, "even more info"),
DomainClass("object3", false, false, "all the info")
).observable()
var lst: ListView<DomainClass> by singleAssign()
override val root = vbox {
lst = listview(listSource) {
cellFormat {
graphic = cache {
hbox {
textfield(it.name)
combobox<Boolean> {
selectionModel.select(it.flag1)
}
combobox<Boolean> {
selectionModel.select(it.flag2)
}
textfield(it.info)
}
}
}
}
button("delete") {
action {
listSource.remove(lst.selectedItem)
}
}
}
}
Any help greatly appreciated!
The suggestion from #Edvin Syse to remove the cache block fixed this for me (although note that he also said a more performant fix would be to implement a ListCellFragment, which I haven't done here):
....
lst = listview(listSource) {
cellFormat {
graphic = hbox {
textfield(it.name)
combobox<Boolean> {
selectionModel.select(it.flag1)
}
combobox<Boolean> {
selectionModel.select(it.flag2)
}
textfield(it.info)
}
}
}
I noticed that the ComboBoxes don't show any other selectable values besides it.flag1 and flag2. You'll want to set the values property to true/false or true/false/null. You can then set the value item directly.
lst = listview(listSource) {
cellFormat {
graphic = hbox {
textfield(it.name)
combobox(values=listOf(true, false)) {
value = it.flag1
}
combobox(values=listOf(true, false)) {
value = it.flag2
}
textfield(it.info)
}
}
}

Is there a way to let Apollo Client globally insert empty strings during loading?

I'm using Apollo Client to receive the GraphQL data for my application. Over time, I see a pattern emerging where for every value I'm querying, I have to include a conditional statement to handle the moment where my data is still loading.
Assume a query looks like this:
query TestQuery($userId: Int!) {
getUser(id: $userId) {
name
}
}
Then, in every place where I want to display the user name, I have to write something like:
{ !this.props.data.loading && this.props.data.getUser.name }
or
{ this.props.data.getUser && this.props.data.getUser.name }
I don't want to display "Loading..." or a rotating spinner in any of these places. Is there a way to avoid this conditional statement by globally replacing all this.props.data.x.y.z values with null or an empty String during loading?
If so, how? Would this be considered an antipattern or bad practice?
If not, which of the above two forms is preferred?
Thanks.
How about this approach?
class GraphqlComponent extends React.Component {
renderError(){
// ...
}
renderLoading(){
// ...
}
renderLoaded(){
}
render(){
const { loading, error } = this.props;
if(error){
return renderError();
}
if(loading){
return renderLoading();
}
return renderLoaded();
}
}
class MyComponent extends GraphqlComponent{
renderLoaded(){
// your logic goes here
}
}

jstree toggle actions for each level

<script type="text/javascript">
//trying to mimmick dee's answer here: http://stackoverflow.com/questions/32290570/lazy-loading-treeview-with-jstree-in-asp-net-mvc
$(function() {
var $children = $("#object-children-tree");
$children.jstree({
"core": {
"animation": 0,
"data": {
"url": function(node) {
return '#Url.Action("GetNodes", "BatchData")';
},
"data": function (selectedNode) {
// Hopefully, each time jstree needs to make an AJAX call this function will be called.
// # is the special ID that the function receives when jstree needs to load the root nodes.
if (selectedNode.id == "#")
return { "selectedNodeId": selectedNode.id }
if (selectedNode.data.nodeType == "provider")
return { "selectedNodeId": selectedNode.id, "selectedNodeType": selectedNode.data.nodeType }
if (selectedNode.data.nodeType == "fileType")
return { "selectedNodeType": selectedNode.data.nodeType, "blockId": selectedNode.data.blockId, "selectedNodeParentId": selectedNode.parent }
return {"selectedNodeId": selectedNode.Id}
}
}
},
"plugins": ["wholerow"]
});
});
</script>
<div id="object-children-tree">
#* Content will be populated by jsTree *#
</div>
I have been reading the jstree documentation and threads on ajax and callback. However, I can't figure out how to apply them to my problem. My tree has five levels. When I toggle a node on level one, I want it to call a particular server action ("/controller/action" b/c I'm using MVC). Then level two children should appear, being created from the results from the server. When I toggle a node on level two, I want it to call a different server action ("/controller/differentAction" - MVC). Then level three children should appear. And so on: toggle a level three node and a different action is called and the response is used to generate level four children. If this has certainly been answered, can you direct me where to look, and possibly explain how it applies? I think the difference with my example is the there is a different action called by toggling a node on each level. I need to do this because the number of children for each node is very great.
Thanks,
**EDIT: ** I have forgone the idea of calling different actions and just allow a particular action to handle which other action to call, which is fine.
[HttpGet]
public ActionResult GetNodes(string selectedNodeId = null, string selectedNodeType = null, string blockId = null, string selectedNodeParentId = null)
{
if (selectedNodeId == "#") //Root nodes
{
return AllProviders();
}
if (selectedNodeType == "provider")
{
return FileTypesOfProvider(Convert.ToInt32(selectedNodeId));
}
if (selectedNodeType == "fileType")
{
return FilesOfProviderOfType(Convert.ToInt32(blockId), Convert.ToInt32(selectedNodeParentId));
}
return AllProviders();
}
I misnamed the parent of actualFiles causing duplicate id's. It is functional now that I fixed that.

Resources