George Opritescu

Developer from somewhere

react import type defined in another file

Problem: wanted to use a type defined in another file. Received following message: Named import from module ‘../mod.js’ ‘MyType’ is a type, but not a value.In order to import it, please use ‘import type’

Solution:

import type {MyType} from '../mod'
Read More

golang redirect os.Stdout

Problem: want to redirect os.Stdout and access it’s contents from a string variable

Solution: this answer proved very useful:

package main

import (
    "bytes"
    "fmt"
    "io"
    "os"
)

func print() {
    fmt.Println("output")
}

func main() {
    old := os.Stdout // keep backup of the real stdout
    r, w, _ := os.Pipe()
    os.Stdout = w

    print()

    outC := make(chan string)
    // copy the output in a separate goroutine so printing can't block indefinitely
    go func() {
        var buf bytes.Buffer
        io.Copy(&buf, r)
        outC <- buf.String()
    }()

    // back to normal state
    w.Close()
    os.Stdout = old // restoring the real stdout
    out := <-outC

    // reading our temp stdout
    fmt.Println("previous output:")
    fmt.Print(out)
}
Read More

async await in mocha tests

Problem: use async-await in mocha tests. Had an async test, and received this error: Error: Resolution method is overspecified. Specify a callback or return a Promise; not both.

Solution: good example here:

it("should work", done => {
  (async () => {
    await something;
    done();
  })();
});

Also, I had to require babel-polyfill in my test instruction:

./node_modules/mocha/bin/mocha –compilers js:babel-core/register –require babel-polyfill test

Read More

react is changing an uncontrolled input of type text to be controlled

Problem: received following message in dev tools: MyClass is changing an uncontrolled input of type text to be controlled. Input elements should not switch from uncontrolled to controlled (or vice versa). Decide between using a controlled or uncontrolled input element for the lifetime of the component

Solution: make sure the property is specified in the initial state. Most likely your initial state does not include it. More info here

Read More

python selenium webdriver set cookie

Problem: use provided cookies with selenium python bindings

Solution: found here:

from selenium import webdriver

driver = webdriver.PhantomJS()
driver.get('http://stackoverflow.com/')

cookies = driver.get_cookies()

driver.delete_all_cookies()

for cookie in cookies :
    driver.add_cookie({k: cookie[k] for k in ('name', 'value', 'domain', 'path', 'expiry')})
Read More

selenium webdriver click select option

Problem: select a specific option in a select dropdown

Solution: found here:

driver.findElement(wd.By.css('#month>option[title=\'November\']')).click();
Read More

golang include sha information in your binary

Problem: you want to include the git SHA in your binary

Solution: Assuming the following code:

package main

var appVersion string

func main() {
  fmt.Println("running version:", appVersion)
}
  • you can run the binary like this: go run -ldflags “-X main.appVersion=$(git rev-parse HEAD)” main.go
  • you can build it like this: go build -ldflags “-X main.appVersion=$(git rev-parse HEAD)” main.go
Read More
go

osx remove zombie processes

Problem: zombie process appeared in ps

Solution: find it’s parent process,and kill it

ps -xo pid,ppid,stat,command | grep your_zombie_process_name
kill -9 ppid_of_zombie_process

Found this useful information here

Read More
osx

xvfb run something with custom screen size

Problem: ran selenium integration tests. They pass locally, but fail that some element is not visible. Took a browser screenshot, turned out that the browser window was very small, and the element was not in view.

Solution: run xvfb-run with custom args: <pre>xvfb-run –server-args=”-screen 0, 1920x1080x24” npm run test</pre>. Solution found here

Read More

selenium webdriver javascript save screenshot

Problem: obtaining a screenshot from selenium webdriver Solution: The string that comes back as a result of takeScreenshot is a base64 png image, so we decode it before writing it to disk:

let browser = new webdriver.Builder().forBrowser('chrome').build()
...
browser.takeScreenshot().then(screenShotContents => {
  const fs = require('fs')
  fs.writeFileSync("screenshot.png", Buffer.from(screenShotContents, 'base64'))
})
Read More