JavaScript Expect selected text

I can simulate text selection with protractor by a lot of different ways. But I can't find solution how I can expect that text was really selected. How I can do this without using marking?

I can simulate select text by protractor with:

  1. DragAndDrop function (mouse)
  2. Combination: browser.actions().sendKeys(protractor.Key.CONTROL, 'a').perform();
  3. Combination: shift + left arrow
Answer:1

Elaborating Michael's comment into a working example.

In the following demo example, we are navigating to the AngularJS website, sending "testing" text to the search input on the top right, pressing left arrow 3 times to move the cursor 3 characters to the left, then pressing SHIFT + ARROW_RIGHT keyboard combination 2 times to select the next 2 characters - which is in: enter image description here

Finally, we are applying the solution to get the selection text provided here and asserting:

describe("Get selection text", function () {
    beforeEach(function () {
        browser.get("https://angularjs.org/");
    });

    it("should input 'testing', select 'in' and assert it is selected", function () {
        var q = element(by.name("as_q"));

        // enter 'testing' and go 3 chars left
        q.sendKeys("testing")
            .sendKeys(protractor.Key.ARROW_LEFT)
            .sendKeys(protractor.Key.ARROW_LEFT)
            .sendKeys(protractor.Key.ARROW_LEFT);

        // select 2 chars to the right
        browser.actions()
            .keyDown(protractor.Key.SHIFT)
            .sendKeys(protractor.Key.ARROW_RIGHT)
            .sendKeys(protractor.Key.ARROW_RIGHT)
            .keyUp(protractor.Key.SHIFT)
            .perform();

        // get highlighted text
        var highligtedText = browser.executeScript(function getSelectionText() {
            var text = "";
            if (window.getSelection) {
                text = window.getSelection().toString();
            } else if (document.selection && document.selection.type != "Control") {
                text = document.selection.createRange().text;
            }
            return text;
        });

        expect(highligtedText).toEqual("in");
    });
});

Hope this would help you and others coming here.

Answer:2

I'm trying to make a WkWebView open a keyboard for tel text input programatically after the WkWebView sends the contained webpage a javascript function call. After this call activates a certain input ...

I'm trying to make a WkWebView open a keyboard for tel text input programatically after the WkWebView sends the contained webpage a javascript function call. After this call activates a certain input ...

  1. wkwebview cangoback
  2. wkwebview cancel load
  3. wkwebview cannot load url
  4. wkwebview canperformaction
  5. wkwebview canvas
  6. wkwebview cancel navigation
  7. wkwebview cangoback not working
  8. wkwebview can't open external links
  9. wkwebview cannot scroll
  10. wkwebview cancel request
  11. wkwebview can't find variable
  12. wkwebview cangoback kvo
  13. wkwebview cangoforward
  14. wkwebview canopenurl
  15. wkwebview cangoback false
  16. wkwebview can't find variable webkit
  17. wkwebview caninitwithrequest
  18. wkwebview configuration cannot be nil
  19. wkwebview xmlhttprequest cannot load
  20. ios wkwebview cangoback

I had majority of these functions working and now when i restarted my program they are saying undefined functions through chrome. Not sure the exact problem, im sure im missing something somewhere but ...

I had majority of these functions working and now when i restarted my program they are saying undefined functions through chrome. Not sure the exact problem, im sure im missing something somewhere but ...

On my website, I'd like divB to be printed right after divA. But if the content of divB implies divB is printed on two pages, I'd like to insert a page break before so that divB is not printed on two ...

On my website, I'd like divB to be printed right after divA. But if the content of divB implies divB is printed on two pages, I'd like to insert a page break before so that divB is not printed on two ...

  1. prevent inline block from wrapping
  2. magento prevent block from caching
  3. css prevent inline-block from wrapping
  4. prevent inline block elements from wrapping
  5. prevent inline-block from wrapping

I want the div to move left to right on scroll. Which I did. But, how do I make the div go to the right much faster? Because the speed of the div going from left to right is the same as the ...

I want the div to move left to right on scroll. Which I did. But, how do I make the div go to the right much faster? Because the speed of the div going from left to right is the same as the ...

  1. make mouse move left to right
  2. make parabola move left and right
  3. unity make object move left and right